File upload with FileSystem in laravel5.1?

I want to upload a file to laravel

, I am only using local server, not cloud. From the doc I found two methods related to file upload,

  • The usual method (Request::file('file')->move(dest,filename))

  • Using File System

But I couldn't figure out the actual difference between these methods?

If I use the normal method,

$file->move('path', $fileName);

      

Here, where should the path be, storage/app

or public/uploads

(new)?

How do I upload files using File System

?

+3


source to share


2 answers


The "normal" method allows you to save the uploaded file only on the local file system anywhere you choose, as long as PHP has write access to that location. This method can only be used for "save" and only for downloaded files.

The "filesystem" method is more flexible because it adds a layer of abstraction over the place where you write to. The filesystem configuration is kept in a separate configuration and is transparent to your code. You can easily change the underlying storage (from local to cloud for example) or change paths without any code changes. The file system also provides you with many additional helper methods for working with the files it stores, such as listing all files, checking for existence, deleting. An added benefit is that it can be used for any files, not just those that were uploaded by the user during the current request.

To answer your second question, you decide where to store files in both the regular and file method. In the normal method you pass the path, in the filesystem you set up the paths using the filesystems.php config.



And how do I load a file using file systems? You are not using the file system to download the file, you are using it to save the downloaded file. The upload process is the same, but instead of calling $ uploadedFile-> move () :

Storage::put('file key', file_get_contents(Request::file('file')->getRealPath()));

      

+4


source


the difference between these two methods is the use.

You are using the File System service to work with your local / cloud file system, such as creating, moving or deleting a file. You can use the Storage :: put () method to store the uploaded file, of course, but you will need to get the file from the request anyway. So you usually just use the file $ request-> ('photo') β†’ move ($ destinationPath); method given at http://laravel.com/docs/5.1/requests#files to move the file wherever you want. The file system service is not designed to handle the downloads themselves. This is the request.



The question of where you put the files is one that you have to answer yourself. By default, storage / application is used to store files. You can put them in public / downloadable files, but this is discouraging since anyone who knows the url can download files. It really depends on what the file is for. If it's a profile picture, it can be published / uploaded. Whether the file is private, then it should not be put, but instead in the storage / application.

+1


source







All Articles