Saturday, 10 February 2018

How to upload files in Laravel ?

Today we are going to see how to upload single or multiples files in Laravel. Here when I use word "files" that means it can be anything like images, videos , audios or any other pdf or word document.To learn how to upload files in Laravel, first you need to setup Laravel if you have not yet.You can see here to setup your Laravel - "How to setup Laravel".

how to upload files in Laravel

How to upload files in Laravel ?

I am just deviding this upload process in three simple steps like,
1) Your View File ( Laravel blade file)
2) Your Route File
3) Your Controller

As we are uploading files and it could be large in case of videos, so first you need to open "php.ini" file which will be at xampp/php folder.If you can not find it then go to the folder and search for "php.ini".Inside that you have increase size of your post data and file upload,

search for "post_max_size" and "upload_max_filesize", you need to increase size for this two, like make it 50mb or 100mb or if you want to upload 200mb video then make it greater than 200 mb otherwise you will get error.

1) Your View File (Laravel blade file)
I am sure you have your HTML ready in your blade file to upload any file.I am putting my code as below.

For Single File Upload:
{!! Form::open(['url'=>'upload','files'=>true]) !!}
{!! Form::file('file_name') !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}

For Multiple Files Upload:
{!! Form::open(['url'=>'upload','files'=>true]) !!}
{!! Form::file('file_name[]',['multiple']) !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}

If you get error like "Class Form Not Found" then follow below simple steps,

1) Add below line of code to your composer.json file in require section.
"laravelcollective/html": "~5.0"

2) Open your project's root directory in cmd and run below command,
composer update

3)Add below line of code to providers section in your config/app.php file,
'Collective\Html\HtmlServiceProvider',

4) Add below line of code to aliases section in your config/app.php file,
'Form' => 'Collective\Html\FormFacade',
'Html' => 'Collective\Html\HtmlFacade',

2) Your Route File
As we have used "upload" post url in our form , let's define route for that as below,
Route::post('upload','UploadController@upload');

3) Your Controller
Here we will write code as below to upload our file.Make one folder "files" inside your public folder of root directory.
public function upload(Request $request)
{
     //For Single File Upload:
        $file = $request->file('file_name');
        $destination_path = public_path().'/files';
        $extension = $file->getClientOriginalExtension();
        $files = $file->getClientOriginalName();
        $fileName = $files.'_'.time().'.'.$extension;
        $file->move($destination_path,$fileName);
     //ends

   //For Multiple Files Upload:
       for($i = 0;$i < count($request->file('file_name'));$i++)
        {

        $file = $request->file('file_name')[$i];
        $destination_path = public_path().'/files';
        $extension = $file->getClientOriginalExtension();
        $files = $file->getClientOriginalName();
        $fileName = $files.'_'.time().'.'.$extension;
        $file->move($destination_path,$fileName);

        }
     //ends
        return redirect()->back();
}

Video Tutorial for uploading files in Laravel

Previous Post
Next Post

post written by: Dhaval Thakkar

Dhaval Thakkar is an enthusiastic web developer who is passionate about learning new things and always ready to help other developers.Just Web Code is a way to provide web solutions to web developers.

0 comments: