简体   繁体   中英

Laravel storage upload Image and File

I'm trying to save an image that is coming from my request in my storage, but something strange is happening: I have the following code

$ request-> file ('UploadIMG') -> store ('public'. '/'. 'clients');

when it is executed it saves the image in the following location

storage \ app \ public \ clients \ dUpBTL0V25h2Q1825IUmvvIZhcwVPixLaVDdItuG.jpg

I wanted to change this value and set a name, so I used the following code

Storage :: disk ('local') -> put ('clients'.'/'.$ fileName, $Image, 'public');

let's say my $ fileName has the value of 01.jpg when I run that storage it creates

storage \ app \ clients \ 01.jpg \ dUpBTL0V25h2Q1825IUmvvIZhcwVPixLaVDdItuG.jpg

it ends up creating a folder with the name of the image and places the image with the random name, how do I get a name for my image?

    $file = $request->file('pic');
    $picName = $file->getClientOriginalName();
    $imagePath = '/public/clients';
    $file->move(public_path($imagePath), $picName);

您可以使用storeAs()方法保存上传文件并为其命名:

$request->file('UploadIMG')->storeAs('public/clients', $fileName);

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