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

这里的技术是共享的

You are here

For this tutorial what we want to achieve is to have a file upload and download system in our Laravel 5 application using the new Storage features. The example is intentionally easy but you can easily extend the concept

shiping1 的头像
For this tutorial what we want to achieve is to have a file upload and download system in our Laravel 5application using the new Storage features. The example is intentionally easy but you can easily extend the concept.

For the purpose we are going to create a database table where we store the file informations. This is needed when we  download the file from the application. With the abstraction of the Storage API you can save the file where you like, locally on the local disk like in the example or Amazon S3 servers, or others.

 

Let’s begin creating a table migration for our fileentries model.

php artisan make:migration create_fileentries_table --table="fileentries"

Edit the code of the created file and add the fields we need.

Migrate the DB and go on :

php artisan migrate

To get advantage from the ORM feature let’s create also the model for our fileentries.

php artisan make:model Fileentry

Before implementing the controller and the view, to get an idea of what we need is better to define the routes needed. We need basically 2 route, one for adding file entries, one for download it. We are going to add a third route to have an index page with a form and where we will display our files.

With our routes in place let’s create the controller.

php artisan make:controller FileEntryController --plain

The index method of the controller will render only the view where we have the form for the upload and a list of the image.

Upload a file to the Storage

The add method have to make two things, one is to save our file to the Storage, other is to insert a row into DB to take track of the file info.

Below the first part of the controller code :

And the code for the form  with a first list of the files name. We will modify this part later adding a display for the pictures.

At this point we are able to upload files to the app and save them into the Storage. But how to vew or download this files ? We didn’t save it int he public directory so we need a method to get it retrieved from the Storage and the present to the broswer.

Download files from Storage

To get the file back to the storage and also have it in response we saved a row in the database. On this example we retrieve the file based on it random name assigned but easily it can be changed to get it via the original name or the id.

It’s important to note that we saved in the database also the mime type of the file. It’s important now to geenrate the correct response header, adding this method to the controller we are now able to retrieve the files.

To complete the tutorial we can implement a list of the picture calling for every picture the method to retrieve the image. Change the index.blade.php to this

 

 Conclusion

That’s a first step in the file managment for a Laravel 5 application. Using the Storage API we can abstract our filesystem and we can save the files where we like or where it’s better for our app. In a future tutorial i would like to cover the file upload and retrieve with a single page application made withAngularJS.

If you want to take a look at the code you can check my repo on github where almost all of the tutorial are saved.

https://github.com/andbet39/todoApp

 

 
Immagine Avatar
Partecipa alla discussione...

 

 
 

 


普通分类: