How To Create File Upload With Laravel

How To Create File Upload With Laravel

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">
  {{ Form::open(array('url'=>'form-submit','files'=>true)) }}
  {{ Form::label('file','File',array('id'=>'','class'=>'')) }}
  {{ Form::file('file','',array('id'=>'','class'=>'')) }}
  <!-- submit buttons -->
  {{ Form::submit('Save') }}
  <!-- reset buttons -->
  {{ Form::reset('Reset') }}
  {{ Form::close() }}
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());


  1. hey,
    you can also utilize the storage_path to get the path to the storage directory so your final code can be like


    so that if you change the path of your storage you dont need to change your code later .

  2. I am working on a file upload form and I am running into an issue where if a user uploads a file with the same name, it overwrites the existing file in storage. I am using the getClientOriginalName() function, but was wondering how to make sure that if the file already exists, to append a character to the filename so that it does not overwrite the existing file. Is this possible?

    • I was able to solve my issue. I added a “str_random(8)” parameter to my file path so that the file names could potentially be duplicates but the file will stay safe.

    • Yes, it is possible you can use \Illuminate\Filesystem\Filesystem class to list all files in the directory using allFiles(string $directory) method and check if file name already exist but I think you should create two storages, the first where client upload files to and then application change file name to a long secure name saved in database and file transferred to the second storage to be safe.

  3. Hi!! I made an upload function using Laravel and Windows 7! But I have a problem, i can’t upload audio or video files there’s only images that are uploaded. Does anyone know why?

  4. Thank you so much, this is all i need to get all the details of the files i’ve selected , cheers!

  5. Thks a lot for the tutorial it is so useful!!! i need to store de file as blob, but this is a good begining 😀

  6. when i use
    {{ HTML::image(storage_path() . ‘\\app\\’ . $user->original_filename) }}
    it generate the src like this

    any idea how to solve it? tnx…

  7. like this


  8. Hi I’m new to laravel 4 and i am trying to upload a xlsx file .After upload a file and click on submit i need to see all the information of that file in a function in the controller.Please suggest how it can be .

Comments are closed.