Laravel 4 – moving uploaded files

Note: This article was written for Laravel 4.x and now very outdated. Please see the comments below for recommendations on other resources you can use for later versions of Laravel.

When handling uploaded files in the controller, there is a point where the file needs to be moved to its final destination. Unusually for Laravel, exceptions need to be used to ensure the move took place.

First check a file has been uploaded. In this example ‘file’ is the name of the file field in the form that it was uploaded through:

if (Input::hasFile('file')) {

    // process the uploaded file

}

Before the file gets moved, you can get some properties of the file like this:

$filename = Input::file('file')->getClientOriginalName();
$extension = Input::file('file')->getClientOriginalExtension();

At this stage the file is still in its source location, a temporary file. You will need to move it to its destination to keep it beyond this page request. The file is moved like this:

$file = Input::file('file)->move($destination_path, $destination_filename);

Where the destination path and filename come from is out of scope for this article, but it could be in the uploads directory, or anywhere that serves its purpose.

If the file move fails, then an exception will be generated. This needs to be caught if you want to handle it:

try {

    $file = Input::file('file')->move($destination_path, $destination_filename);

} catch(Exception $e) {

    // Handle your error here.
    // You might want to log $e->getMessage() as that will tell you why the file failed to move.
}

Now an important point to note: after the move, Input::file(‘file’) no longer exists. You can no longer check its size, get its filename etc. Instead, use $file, as that is now the object for the file in its new location.

—-

Thanks for the super-speedy help on Laravel IRC for this. The help available there never fails to impress me.

7 Responses to Laravel 4 – moving uploaded files

  1. MinhQuy 2014-06-20 at 05:40 #

    Thank for guide. It you write a guide of Laravel 4 upload image with preview image and how to up load multiple file in laravel 4.

  2. MinhQuy 2014-06-20 at 06:30 #

    Route :
    Route::get(‘upload’, ‘UploadController@getUploadFile’);

    Route::post(‘upload’, ‘UploadController@postUploadFile’);
    Controller : UploadController.php
    getClientOriginalName();
    // get extension file
    $extension = Input::file(‘image’)->getClientOriginalExtension();
    try {
    // move file to directory images in public folder
    Input::file(‘image’)->move($destinationPath, $fileName);
    } catch (Exception $e) {
    // Handle your error here.
    // You might want to log $e->getMessage() as that will tell you why the file failed to move.
    }
    }
    }
    }

    and View: add option array multiple in Form:file.
    {{Form::open(array(‘enctype’ => ‘multipart/form-data’))}}
    {{Form::label(‘image’, ‘Upload’)}}
    {{Form::file(‘image’, array(‘multiple’))}}
    {{Form::submit(‘Upload’)}}
    {{Form::close()}}

  3. MinhQuy 2014-06-20 at 06:41 #

    so i want fecth one by one file to move destination folder ????

  4. budi 2014-10-21 at 11:42 #

    nice. but sadly there’s no validation

    • Jason Judge 2014-10-22 at 11:01 #

      Absolutely true, and good catch. Like any other user input (GET, POST, COOKIES etc), files cannot be trusted until after you have thoroughly checked them over – look at MIME, extension, file size, and then perhaps pass it through some filters to check them for various hacks such as PHP code embedded into the header of a GIFs. This article purposely just covers the mechanics of how to get at the file and how to move it to somewhere so that it can be further processed.

Leave a Reply