19

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?

jrenk
  • 1,387
  • 3
  • 24
  • 46
  • 1
    The file exists? Is it uploaded? Can you post what's inside `Input::file()`. Use `dd()` for var_dumping. – toesslab Sep 19 '14 at 08:18
  • 1
    I solved what was wrong. The code is fine, but the problem was some pictures were simply to big. – jrenk Sep 19 '14 at 13:58

4 Answers4

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.

jrenk
  • 1,387
  • 3
  • 24
  • 46
  • 1
    why is a big picture returning hasfile() -> false? – Jorre Jul 10 '15 at 16:28
  • @Jorre I can't answer that ... But everything worked on smaller pictures. – jrenk Jul 15 '15 at 12:52
  • 6
    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. – Don't Panic Jul 17 '16 at 21:47
  • 1
    One thing important to note is I also had to change post_max_size to be greater than upload_max_filesize. – rick6 May 07 '20 at 16:42
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

Felipe Castillo
  • 536
  • 8
  • 25
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

Tim
  • 5,893
  • 3
  • 35
  • 64
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

Ngugi Kiarie
  • 1,353
  • 11
  • 9