欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

How To Create File Upload With Laravel

shiping1 的头像

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.

<!--app/views/form.blade.php-->
<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <title>
   Laravel
  </title>
 </head>
 <body>
  {{ Form::open(array('url'=>'form-submit','files'=>true)) }}
  
  {{ Form::label('file','File',array('id'=>'','class'=>'')) }}
  {{ Form::file('file','',array('id'=>'','class'=>'')) }}
  <br/>
  <!-- submit buttons -->
  {{ Form::submit('Save') }}
  
  <!-- reset buttons -->
  {{ Form::reset('Reset') }}
  
  {{ Form::close() }}
 </body>
</html>
//app/routes.php
Route::get('form', function(){
 return View::make('form');
});

Route::any('form-submit', function(){
 var_dump(Input::file('file'));
});

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.

object(Symfony\Component\HttpFoundation\File\UploadedFile)[9]
  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. ThegetFilename() 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());
});

24 COMMENTS.

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

    Input::file(‘file’)->move(storage_path(),Input::file(‘file’)->getClientOriginalName());

    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?
    Thanks!

  4. Thks a lot for the tutorial it is so useful!!! i need to store de file as blob, but this is a good begining </body></html>

普通分类: