I basically just want to get the name of a file which I get like this:
$inputPdf = $request->file('input_pdf');
if I dd($inputPdf)
it prints me null
.
The hashName method is exactly what Laravel calls in the store method. $request->image->hashName(); You will get the same name that Laravel generates when it creates the file name during the store method.
To get the full name of a file without its extension in PHP use the pathinfo() function. Pass the file name as the first argument and PATHINFO_FILENAME as the second.
To get the extension of a file in PHP/Laravel, use the PHP pathinfo() function. The first argument is the name of the file and the second will be what part of the file to get, which is this case is PATHINFO_EXTENSION . $extension = pathinfo($input_file, PATHINFO_EXTENSION);
The function GetClientOriginalName() is used to retrieve the file's original name at the time of upload in laravel and that'll only be possible if the data is sent as array and not as a string. Hence, you must add enctype="multipart/form-data" whenever you are creating a form with input fields for files or images.
You may access uploaded files that are included with the Illuminate\Http\Request instance using the file method. The object returned by the file method is an instance of the Symfony\Component\HttpFoundation\File\UploadedFile class, which extends the PHP SplFileInfo class and provides a variety of methods for interacting with the file
There are a variety of other methods available on UploadedFile instances. Check out the API documentation for the class for more information regarding these methods.
So you can use this method: getClientOriginalName()
http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html#method_getClientOriginalName
$request->file('input_pdf')->getClientOriginalName();
Would return the file name.
You can do this to check if the file exists before calling any methods on it:
if ($request->hasFile('input_pdf')) { return $request->file('input_pdf')->getClientOriginalName(); } else { return 'no file!' }
dd($request->file('input_pdf'))
returning null
check you are using the correct name for the file. You can try dd($request)
and you will see if there are any files in it. You can check the file name when reviewing the dump of the Request
object.$request->file->getClientOriginalName();
Check this for more methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With