Laravel : To rename an uploaded file automatically

后端 未结 7 1342
北海茫月
北海茫月 2020-12-11 04:31

I am allowing users to upload any kind of file on my page, but there might be a clash in names of files. So, I want to rename the file automatically, so that anytime any fil

7条回答
  •  既然无缘
    2020-12-11 05:04

    You can rename your uploaded file as you want . you can use either move or storeAs method with appropiate param.

    $destinationPath = 'uploads';
    $file = $request->file('product_image');
    foreach($file as $singleFile){
        $original_name = strtolower(trim($singleFile->getClientOriginalName()));
        $file_name = time().rand(100,999).$original_name;
         // use one of following 
        // $singleFile->move($destinationPath,$file_name); public folder
        // $singleFile->storeAs('product',$file_name);  storage folder
        $fileArray[] = $file_name;
    }
    print_r($fileArray);
    

提交回复
热议问题