Laravel: Upload & Download from Storage folder

Posted: July 10, 2016 in Laravel
Tags:

Hi, here is the basic of upload and download multiple files to storage folder in Laravel.

First, create new upload folder in your Storage –> /storage/uploads

Routes:

#route to upload files
Route::post( 'upload',   ['as' => 'upload',       'uses' => 'UploadController@upload_file']);
#route to download file
Route::get('get-file/{filename}', ['as' => 'getFile', 'uses' => 'UploadController@get_file']);

 

Basic Upload Form:

{!! Form::model( $model, ['route' => ['upload', $model->id], 'method' => 'POST', 'id' => 'form_upload', 'files'=>true] ) !!}
{!! Form::file('files[]', array('multiple'=>true)) !!}
{!! Form::submit('Submit', array('class'=>'send-btn')) !!}
{!! Form::close() !!}


Controller:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Str;

class UploadController extends Controller
{
   /**
   * Upload File
   **/
   public function upload_file() {
     $destination_path = storage_path('uploads');
     $files = $request->file('files');
     foreach($files as $file) {
         $validator = Validator::make(
                      [
                          'file' => $file,
                          'extension' => Str::lower($file->getClientOriginalExtension()),
                      ],
                      [
                          'file' => 'required|max:100000',
                          'extension' => 'required|in:jpg,jpeg,bmp,png,doc,docx,zip,rar,pdf,rtf,xlsx,xls,txt, csv'
                      ]
                     );
 
         if($validator->passes()){
             $filename = $file->getClientOriginalName();
             $upload_success = $file->move($destination_path, $filename);
             if ($upload_success) {
                     #if needed, save to your table
                     $attach = new attachments();
                     $attach->file_name = $filename;
                     $attach->mime = $file->getClientMimeType();
                     $attach->save();
             }
         }
     }
  }

  /**
  * Download file
  **/
  public function get_file($filename)
  {
        $file_path = storage_path('uploads') . "/" . $filename;
        return Response::download($file_path);
  }
}

 

View & Download Attachments:

@foreach($attachments as $attach)
  <tr>
       <td>{!! link_to( route('getFile', $attach->filename) . '/' . $attach->file_name, $attach->file_name) !!} </td>
  </tr>
@endforeach

 

Thats all.. Good Luck.. ok Bye!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s