Avoiding exceptions when uploading files in 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:


Here is the text of the exception:

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?


PHP’s $_FILES will have the tmp_name key in it regardless of whether a file is uploaded or not. It’s sent as an empty string though, not NULL. It appears has_file() expects NULL:

public static function has_file($key)
    return ! is_null(static::file("{$key}.tmp_name"));

To work around it in your controller you could try:

if (array_get(Input::file('attachment1'), 'tmp_name'))
    // file uploaded