Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload file and save it with original name in storage folder

I want save uploaded file with original name.What i must add to this code?

Below's my code

public function store(Request $request)
{
    if($request->hasFile('image'))
    {
        $file = $request->file('image');
        $originalname = $file->getClientOriginalName();
        $filename =$originalname;
        $file->move('public/', $filename);
    }
like image 924
Kira Avatar asked Sep 06 '25 03:09

Kira


2 Answers

You may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:

public function store(Request $request)
{
    if($request->hasFile('image'))
    {
        $file = $request->file('image');
        $originalname = $file->getClientOriginalName();
        $path = $file->storeAs('public/', $originalname);
    }
}
like image 167
DPS Avatar answered Sep 07 '25 20:09

DPS


Upload files with original fileName.

if ($request->hasFile('image')) {
    $file_path = $request->file('image')->storeAs('dir_name', request()->file('image')->getClientOriginalName(), 'optional_disk_name');
}

// pass the disk name as the third argument to the storeAs method: ex. local,s3,etc
like image 26
dipenparmar12 Avatar answered Sep 07 '25 22:09

dipenparmar12