Marcolino922's avatar

Extract file name just loaded with Storage

Hi, I need to extrapolate the assigned name of the uploaded file, "store" automatically generates a random ID. I would like to extrapolate this random ID, without extension, in order to save it in the database. Unfortunately I could not find anything in the documentation, I did some tests, but nothing.


if($request->hasFile('files_to_download')){

            $upload_file = $request->file('files_to_download')->store('uploads');
            $name = *****************+
            
            $files_to_download = $uploadFile->save([
                $uploadFile->files_to_download = $name,
            ]);
            
        }
0 likes
1 reply
a4ashraf's avatar
a4ashraf
Best Answer
Level 33

@marcolino922

this is the function ow we get the file name

`` $obj->getClientOriginalName();

``

here how I'm using

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

        $file->storeAs('uploads/', $fileName, 'uploads');

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

Please or to participate in this conversation.