Home / Snippets / Laravel get Name of Uploaded File
Laravel get Name of Uploaded File cover

Laravel get Name of Uploaded File

308

2 years ago

0 comments

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';
}
notion avatar

Ahabz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this