如何更改我上传的文件名并且不更改扩展名

时间:2019-05-16 12:46:26

标签: php laravel file

所以我要上传图片,并且要将其名称更改为:

logo_{{Auth::user()->firstName}}_{{Auth::user()->lastName}} 

,我想保留扩展名。 在我的控制器中,我有:

<?php namespace App\Http\Controllers;

use Illuminate\Support\Facades\Input;

class UploadController extends Controller {

    public function upload(){

        if(Input::hasFile('file')){

            echo 'Uploaded';

            $file = Input::file('file');
            $file->move('uploads  ', $file->getClientOriginalName());
            $custom_name = 'logo_{{Auth::user()->firstName}}_{{Auth::user()->lastName}}'.$file->getClientOriginalExtension();
            echo '<img src="uploads/' . $file->getClientOriginalName() . '"/>';
        }

    }
}

所以是的,实际上它是getClientOriginalName,因此它会保持相同的名称,但是我该如何更改呢?

1 个答案:

答案 0 :(得分:1)

我认为您遇到语法问题,请按照以下代码段

$custom_name = "logo_".Auth::user()->firstName."_".Auth::user()->lastName.$file->getClientOriginalExtension();
$file->move(public_path('uploads'), $custom_name); // destination path, name of file