29

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.

Fusion
  • 3,603
  • 5
  • 30
  • 42
John Does Legacy
  • 1,123
  • 6
  • 17
  • 28
  • The input was empty (no file given) or you are no getting the correct input name. – Arcesilas May 11 '16 at 11:44
  • 1
    It might be useful you provide related HTML, or any information. For now, it's not possible to say more than: there is no file. We don't know what you are doing in your browser, what your html looks like, or anything like that. – Arcesilas May 11 '16 at 11:50

3 Answers3

70
  1. To get the file name, as mentioned in the docs:

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!'
}
  1. To solve the issue of 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.
Community
  • 1
  • 1
haakym
  • 10,296
  • 10
  • 56
  • 90
  • This returns me Call to a member function getClientOriginalName() on a non-object – John Does Legacy May 11 '16 at 11:43
  • Which means there was no object returned by using the `file()` method. `$request->file('input_pdf')` returned null, so you're essentially trying to call `getClientOriginalName()` on null. Try `dd($request)` and you will see if there are any files in it. Perhaps you got the wrong name? – haakym May 11 '16 at 11:45
  • 1
    Are you also passing the `Request $request` in your method? – Andy Holmes May 11 '16 at 11:45
  • See my answer's update for checking file is in the request – haakym May 11 '16 at 11:49
  • Can you `dd($request)` and paste the output @JohnDoesLegacy? – Andy Holmes May 11 '16 at 11:49
  • Include your form html with file input, if you're updating your question – haakym May 11 '16 at 11:52
  • 1
    Because none of you read the question entirely: you proposed something which was not relevant: there is no file. Period. This was the main problem, as mentioned in the question. – Arcesilas May 11 '16 at 11:53
  • The question mentions: "if I dd it prints me null." Which means that calling any method obviously will give the error OP reported in first comment on both answers. – Arcesilas May 11 '16 at 11:55
  • @Arcesilas thanks for leaving the comment, I genuinely didn't read the sentence after the code that reads "if I dd it prints me null." and your downvote is valid. Unless he edited that in afterwards? – haakym May 11 '16 at 11:57
  • 1
    No, it was here the first time i opened the question. The question has not been edited at all. – Arcesilas May 11 '16 at 11:57
  • 1
    Ok I think I got it liike you guys said the name of an input was wrong just a syntax thing instead of _ I wrote - I tick this one if it is ok for you – John Does Legacy May 11 '16 at 12:34
  • Cool. Glad you got it working. I'll add the comment I made regarding the issue that fixed it to my answer. – haakym May 11 '16 at 12:43
  • @Arcesilas the updated answer now addresses both parts of the question, thanks – haakym May 11 '16 at 12:51
  • 1
    And thanks again for your comment when I asked about the downvote. Will remind me to read the question properly before answering! – haakym May 11 '16 at 12:57
6
$request->file->getClientOriginalName();

Check this for more methods.

Gerard Reches
  • 2,696
  • 2
  • 21
  • 37
ExohJosh
  • 1,565
  • 12
  • 20
  • This returns me Call to a member function getClientOriginalName() on a non-object – John Does Legacy May 11 '16 at 11:44
  • 1
    Ensure you are posting a file, you can see the contents of the request object by doing dd($request). Also ensure your form (on the front end) is accepting file input (enctype) – ExohJosh May 11 '16 at 11:46
2

I think you forget to add

enctype="multipart/form-data"

In your form. Or if you used laravel HTML service provider use

{!! Form::open(array('files' => true)) !!}
Parvez Rahaman
  • 3,659
  • 2
  • 18
  • 34