A Software Engineer and Occasional Writer.

How To Create File Upload With Laravel

10 April 2014

We discussed before form creation and validation but file upload input is a bit different. Let’s create our first file upload with laravel.

Before retrieving uploaded file data, We need to setup a simple form and two routes. The first route is for form view and the other route for form submission.

<!doctype html>
<html lang="en">
  <meta charset="UTF-8">

  <!-- submit buttons -->

  <!-- reset buttons -->

Route::get('form', function(){
 return View::make('form');

Route::any('form-submit', function(){

Basically, Uploaded files aren’t stored in the $_GET or $_POST array but they stored in the $_FILES array. Fortunately laravel provide a great API to access $_FILES array. To retrieve data of uploaded files, You can use Input::file() method. Let’s dump the value of Input::file() and explore the resulted object.

  private 'test' => boolean false
  private 'originalName' => string 'test.jpg' (length=22)
  private 'mimeType' => string 'image/jpeg' (length=10)
  private 'size' => int 667220
  private 'error' => int 0

Well. You can interact with these data by a bunch of methods. The getFilename() method used to get temporary file name given to our file on temporary location. Let’s try this method.

Route::any('form-submit', function(){
 return Input::file('file')->getFilename();

The getClientOriginalName() method used to get the actual name of the file when it was uploaded.

Route::any('form-submit', function(){
 return Input::file('file')->getClientOriginalName();

The getClientSize() method used to get the size of the uploaded file in bytes.

Route::any('form-submit', function(){
 return Input::file('file')->getClientSize();

The getClientMimeType() method used to get the mime type of uploaded file.

Route::any('form-submit', function(){
 return Input::file('file')->getClientMimeType();

The guessClientExtension() method used to get extension of uploaded file.

Route::any('form-submit', function(){
 return Input::file('file')->guessClientExtension();

The getRealPath() method used to get current location of uploaded file.

Route::any('form-submit', function(){
 return Input::file('file')->getRealPath();

The move() method used used to move uploaded file to another location. The first parameter is the new destination while the second parameter is the new name. Let’s see how method work.

Route::any('form-submit', function(){
 return Input::file('file')->move(__DIR__.'/storage/',Input::file('file')->getClientOriginalName());