Tips: Managing file storage in Laravel 4


Error message

Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in menu_set_active_trail() (line 2405 of /home/vusanico/public_html/includes/

Tips: Managing file storage in Laravel 4

In this tutorial, we are going to upload files and have functionality to delete again using the Laravel PHP framework, most parts in this tutorial use pure PHP whiles some parts require a knowledge of Laravel. We start by creating a Laravel application in a project folder of your choice. If your not familiar with creating Laravel projects refer to the documentation on the Laravel website.

Laravel is a free, open source PHP web application framework, designed for the development of model–view–controller (MVC) web applications.

Once the Laravel application is up and running create a routes to a file upload page I've created /files defined as: 

Route::get('/files', array('as' => 'fileupload', 'uses' => '[email protected]'));
Route::post('/files/upload', array('as' => 'do.fileupload', 'uses' => '[email protected]_create'));

And in the create function inside the FileManagerController simply return a new view

 public function create() {
          return View::make("files.create");

Now create the create.blade.php template in the views folder such as /app/views/files/create.blade.php in this file create a simple form that allows for the uploading of files:

                <div class="panel panel-default">

                    <div class="panel-heading">

                        <h4>Upload the file</h4>


                    <div class="panel-body">


                        <div class="form-group">

                            {{Form::label('datafile','File upload: *')}}



                        {{Form::submit('Update',array('class'=>'btn btn-success'))}}



And now in our post function do_create we are going to handle the file upload:

public function do_create() {

 if(!Input::hasFile("datafile")) { //check if a file is selected to upload
return Redirect::back()->withErrors("Please select a file to upload")->withInput();}

        $file = Input::file("datafile"); //at this point all is well with the file

        if($file->getClientOriginalExtension() != "txt")  { //simple check for file type 

            return Redirect::back()->withErrors("Please upload a valid .txt file")->withInput();


        $dateNow = Carbon::now();

        $storagePath = storage_path().'/filesdata/pvt/';

        $filename = "fileupload-".$dateNow.".txt"; //rename the file

        $upload_success = $file->move($storagePath,$filename); //take to final resting place


Now we have successfully uploaded the onto the server, if you look at the storage_path folder you should see you file that was uploaded, now we want to manage files and be able to delete the files, in order to remove a file from the system that was uploaded we can simply use unlink:

public function rm_file($path) {

$storagePath = storage_path().'/filesdata/pvt/'.$path; //$path is simply the name of the file

        if(File::exists($storagePath))  { //first we check if the file does exist
               unlink($storagePath); //remove the file from the storage_path/the system
               return "File removed :-)";


        return "Sorry that file {$path} could not be found in the system";


And that concludes a very basic file upload and deletion tip using Laravel, there's definitely quite a bit that can be added to improve the code to make it production ready but hopefully this points you to the right direction :-)