Laravel Input::hasFile('image') returns false even if a File is uploaded
Asked Answered
P

4

20

I have a Form field for an Image upload, which I open with 'files' => true, like so:

{{ Form::label('image', 'Image') }}
{{ Form::file('image') }}

And in my Controller I want to check if a File was uploaded and do something with it:

if (Input::hasFile('image')){
        $in_path = 'img/';
        $in_extension = Input::file('image')->getClientOriginalExtension();
        $filename = Input::get('name').".".$in_extension;
        Input::file('image')->move($in_path, $filename);
        $user->image = $filename;
    }

But Input::hasFile always returns false and I don't know why.

Input::file('image');

results in:

Symfony\Component\HttpFoundation\File\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => test.JPG
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => application/octet-stream
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 1
[pathName:SplFileInfo:private] => 
[fileName:SplFileInfo:private] => 
)

I have tested around with another picture for another User and this works fine. I don't get why this is working for some Users and for some others not.
Is there maybe some kind of Pictures that are not accepted?

What other sources could be the cause of this problem?

Pru answered 19/9, 2014 at 8:7 Comment(2)
The file exists? Is it uploaded? Can you post what's inside Input::file(). Use dd() for var_dumping.Nathan
I solved what was wrong. The code is fine, but the problem was some pictures were simply to big.Pru
P
19

I solved what was wrong. The code is fine, but the problem was some pictures were simply to big.

EDIT:
As Don't Panic pointed out, editing upload_max_filesize can solve the problem.

Pru answered 19/9, 2014 at 9:37 Comment(4)
why is a big picture returning hasfile() -> false?Organometallic
@Organometallic I can't answer that ... But everything worked on smaller pictures.Pru
This is not better as a comment just because it's short. It is the answer, and it's perfectly valid. I had the same problem and arrived at the same solution. Increasing upload_max_filesize just a bit took care of it.Nomography
One thing important to note is I also had to change post_max_size to be greater than upload_max_filesize.Insomniac
S
16

I had the same problem, I checked my code and noticed that I had not the enctype="multipart/form-data" header in the form, hope that this big neglect help anyone

Sacks answered 2/11, 2017 at 14:46 Comment(0)
N
7

How do you open your form? If you want your form to accept files you need so open it like this:

echo Form::open(array('url' => 'foo/bar', 'files' => true))

Opening a form in the Laravel Docs

Nearby answered 19/9, 2014 at 8:11 Comment(0)
T
1

For people using later versions of laravel

Add enctype="multipart/form-data" as a form attribute, also ensure that the method is POST

<form action="/foo" method="post" enctype="multipart/form-data">

Regards

Ticonderoga answered 30/11, 2021 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.