简体   繁体   中英

Laravel 5.0: Change the name of an uploaded file

I have a file which I'm uploading, but the name doesn't appear to change. I would like to rename the file which I upload.

if ($request->hasFile('cv')){
    $file=$request->file('cv');
    $fileName= $user->lastName + date("Y-m-d H:i:s");
    $destinationPath=config('app.CVDestinationPath')."/cv";
    $uploaded = Storage::put($destinationPath, file_get_contents($file->getRealPath()));
}

This piece of code works properly in uploading, but it does not rename the file. How would I make the name of it to $fileName?

Add the filename to the destination path like so

// note concatenator in PHP is `.` and not `+`
$fileName= $user->lastName . date("Y-m-d H:i:s");

$destinationPath=config('app.CVDestinationPath')."/cv/$fileName";

Try this:

1) Get file extension

2) Concatenate userlastname , date and file_extension

3) Assign fileName in destinationPath

if ($request->hasFile('cv')){
    $file = $request->file('cv');

    $file_extension = $file->getClientOriginalExtension(); //** get filename extension
    $fileName = $user->lastName . date("Y-m-d H:i:s") . $file_extension;

    $destinationPath = config('app.CVDestinationPath')."/cv/".$fileName;
    $uploaded = Storage::put($destinationPath, file_get_contents($file->getRealPath()));
}

Hope it's helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM