Posts Learn Components Snippets Categories Tags Tools 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

192 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';
}

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

Load comments for Laravel get Name of Uploaded File

)