繁体   English   中英

Laravel 5.0:更改上传文件的名称

[英]Laravel 5.0: Change the name of an uploaded file

我有一个要上传的文件,但名称似乎没有变化。 我想重命名我上传的文件。

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()));
}

这段代码可以在上传中正常运行,但不会重命名文件。 如何将其命名为$ fileName?

像这样将文件名添加到目标路径

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

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

尝试这个:

1)获取file扩展名

2)连接userlastnamedatefile_extension

3)在destinationPath分配fileName

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()));
}

希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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