Laravel 5.0: изменение имени загружаемого файла

У меня есть файл, который я загружаю, но имя не меняется. Я хотел бы переименовать файл, который я загружаю.

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?

0

Решение

Добавьте имя файла к пути назначения так

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

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

Другие решения

Попробуй это:

1) Получить file расширение

2) сцепить userlastname , date а также file_extension

3) Назначить fileName в 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()));
}

Надеюсь, это полезно.

2

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector