coolpraz's avatar

How to get uploaded file name in laravel 5.3

How to get uploaded file name after file was uploaded in laravel 5.3

$path = $request->image->store('media', 'public');
0 likes
12 replies
ctroms's avatar

You can try:

$path = $request->image->getClientOriginalName();

Have a look at the the UploadedFile API Docs class for other methods available to you.

7 likes
coolpraz's avatar

@ctroms

with

$path = $request->image->getClientOriginalName();

you can get image name to be uploaded

but in laravel 5.3 image name converted into md5 hash and saved to disk, the name will be something like this "df91c54c4ad5d78f43704f71d256a5e9.jpeg". My concern is how can i get saved file name in laravel 5.3 and also $path variable return only string of image uploaded path.

Refer to: https://laravel.com/docs/5.3/requests#storing-uploaded-files

1 like
Snapey's avatar

If you do not want a file name to be automatically generated, you may use the storeAs method, which accepts the path, file name, and disk name as its arguments:

Pass the location you want to store the file, and the name of the file. The name could be the getClientOriginalName()

coolpraz's avatar

@Snapey @JeffreyWay

yea, but i want to generate image name automatically, I want laravel to return whole image object instead of just path string and that will be more flexible to work with when you have separate table for image and want to store all image information.

nikocraft's avatar

@coolpraz What exactly is the problem?

How to get uploaded file name in laravel 5.3

getClientOriginalName()

that's how you get the name of uploaded file in laravel 5.3

If you are renaming the file you already have the name so where is the problem?

There is no image object, if that is what you want returned by laravel 5.3

ctroms's avatar

@coolpraz

Are you wanting the path and md5 hashed name before you store the file?

IsaacBen's avatar

You already got the right answers here. This is what I do to get the file and store the file path, file name, and extension.

    public function store(Request $request)
    {
        $file     = request()->file('file');
        $fileName = rand(1, 999) . $file->getClientOriginalName();
        $filePath = "/uploads/" . date("Y") . '/' . date("m") . "/" . $fileName;

        $file->storeAs('uploads/'. date("Y") . '/' . date("m") . '/', $fileName, 'uploads');

        return File::create(['file_name' => $fileName, 'path' => $filePath, 'file_extension' => $file->getClientOriginalExtension()]);
    }
1 like
ejdelmonico's avatar

Also, you have access to getRealPath() which is made available through SplFileInfo. Every uploaded file in Laravel is a descendent of SplFileInfo.

coolpraz's avatar

@maxnb @ctroms

getClientOriginalName() return the name of the image to be uploade not the name of image which is already uploaded by this line.

$path = $request->image->store('media', 'public');

above line of code upload the image into disk and automatically generate name of image using MD5 hash

Refer to documentation of Laravel 5.3: https://laravel.com/docs/5.3/requests#storing-uploaded-files

and what i want is get automatically generated image name.

ctroms's avatar
ctroms
Best Answer
Level 15

@coolpraz

Now I get what you are after.

When you get an uploaded file from a request you get an UploadedFile instance. That object has a method hashName() that generates an md5 has of the file contents. here is a link to the api.

The hashName method is exactly what Laravel calls in the store method.

If you call

$request->image->hashName();

You will get the same name that Laravel generates when it creates the file name during the store method.

21 likes

Please or to participate in this conversation.