Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding exceptions when uploading files in laravel

Tags:

php

laravel

I've got a file upload field (attachment1) in a form that may or may not have a file uploaded in it when I process the form in laravel.

When I'm trying to process the page, this line generates an exception:

Input::upload('attachment1',path('storage').'attachments/'.$name);

Here is the text of the exception:

Message:
Call to a member function move() on a non-object

it seems that I need to check in advance to see if 'attachment1' has a file, and I found that the function Input::has_file('attachment1') is supposed to tell me whether or not 'attachment1' has a file, but even when I submit an empty form, it returns true.

Also, from reading documentation, it seems that Input::upload is supposed to just return false when trying to upload a non-existant file, so why does it produce this exception instead, and how can I fix it?

like image 893
occam98 Avatar asked Jun 21 '12 20:06

occam98


1 Answers

You should try to do some checking before trying to upload the attachment. This works for me:

$file = Input::file('attachment1');
 if (is_array($file) && isset($file['error']) && $file['error'] == 0) {
 Input::upload('attachment1',path('storage').'attachments/'.$name);
}
like image 139
sasham7 Avatar answered Oct 19 '22 23:10

sasham7