Posts Learn Components Snippets Categories Tags About
/

Laravel get Name of Uploaded File

Learn how to get uploaded file name in Laravel using the Request instance

Created on Jul 27, 2021

24 views

To get the name of the uploaded file you can make use of the "getClientOriginalName" provided by the "Illuminate\Http\Request " class.
$posterName = $request->file('poster')->getClientOriginalName();


getClientOriginalName Full Code Example
Below are the full code example that you can refer to.
Route::post('/posts/{id}/store-image', function (int $id, Request $request) {
  $poster = $reqeust->file('poster');

  $posterStream = Image::make($poster)->resize(350, 496)->stream('webp', 80);

  $posterName = '/image/' . $poster->getClientOriginalName() . '.webp';

  Storage::disk('public')->put($posterName, $posterStream, 'public');

  Post::find($id)->update(['poster_image' => $posterName]);
});

Check Uploaded Images
To check if the uploaded image is successful, you can make use of the "hasFile" method.
if ($request->hasFile('poster')) {
    return $request->file('poster')->getClientOriginalName();
} else {
    return 'file not exist';
}

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

Load comments for Laravel get Name of Uploaded File

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that's right for your project.

View Components

Other Code Snippets

More