Home > OS >  How to Save File in storage folder with Original name?
How to Save File in storage folder with Original name?

Time:03-10

I want to store an uploaded file with its original client name in the storage folder. What do I need to add or change in my code?Any help or recommendation will be greatly appreciated

Here my Controller

public function store(Request $request) {
    $path = "dev/table/".$input['id']."";
    $originalName = $request->file->getClientOriginalName();
    $file = $request->file;
    
    
    Storage::disk('local')->put($path, $file);
}

Edit: I know how to get the originalClientName. the problem is storing the file in the folder using the original name, not the hash name.I needed help on this specific part Storage::disk('local')->put($path, $file);

CodePudding user response:

public function store(Request $request) {
    
    $originalName = $request->file->getClientOriginalName();
    $extension = $request->file->getClientOriginalExtension();

    $path = "dev/table/" . $input['id'] . "/" . $originalName . "." . $extension;

    $file = $request->file;
    
    
    Storage::disk('local')->put($path, $file);
}

CodePudding user response:

To get the original file name you can use this in your ControllerClass:
$file = $request->file->getClientOriginalName();

To get additional the extension you can use this Laravel Request Method:

$ext = $request->file->getClientOriginalExtension();

Then you can save with:

$fileName = $file.'.'.$ext;
$request->file->storeAs($path, $fileName);
// or 
Storage::disk('local')->put($path . '/' . $fileName , $request->file);

  • Related