Kira
Kira

Reputation: 85

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);
    }

Upvotes: 4

Views: 12605

Answers (3)

dipenparmar12
dipenparmar12

Reputation: 3455

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

Upvotes: 1

DPS
DPS

Reputation: 1003

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);
    }
}

Upvotes: 7

user10186369
user10186369

Reputation:

Try this:

if($request->hasFile('image'))
{
    $file = $request->file('image');
    $originalname = $file->getClientOriginalName();
    $img = Image::make($file->getRealPath());
    $img->stream();
    Storage::disk('local')->put('images/'.$originalname, $img, 'public');
}

Upvotes: 0

Related Questions