Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 584
jrenk Avatar asked Sep 19 '14 08:09

jrenk


4 Answers

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.

like image 83
jrenk Avatar answered Oct 24 '22 09:10

jrenk


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

like image 33
Felipe Castillo Avatar answered Oct 24 '22 10:10

Felipe Castillo


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

like image 28
Tim Avatar answered Oct 24 '22 10:10

Tim


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

like image 36
Michael Kiarie Avatar answered Oct 24 '22 10:10

Michael Kiarie