gsk
gsk

Reputation: 2379

File upload using FileSystem in laravel5.1?

I want to upload file in laravel, I am using only local server and but not any cloud storage. From doc,I found two methods related with file upload,

But I could not understand actual difference between these methods?

If I am using normal method,

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

Here where should be path located,storage/app or public/uploads(new)?

How can I upload files using File System ?

Upvotes: 3

Views: 1904

Answers (2)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40899

The "normal" method allows you to save the uploaded file only in the local filesystem in any place you choose, as long as PHP has write access to that location. This method can only be used for "save" operation and only for uploaded files.

The "filesystem" method is more flexible as it adds a layer of abstraction over the place you write to. Filesystem configuration is stored in a separate config and is transparent to your code. You can easily change the underlying storage (e.g. from local to cloud) or change paths without any changes to the code. The filesystem also gives you a lot of additional helper methods to operate on files it store, like list all files, checking existence, removing. Additional advantage is that it can be used to any files, not only the ones uploaded by the user during current request.

Answering your second question: you decide where to store files in both normal and filesystem method. In the normal method you pass the path, in filesystem you configure the paths using filesystems.php config.

And how to upload the file using File systems? You don't use the filesystem to upload the file, you use it to save the uploaded file. The upload process is the same, but instead of calling $uploadedFile->move() you do:

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

Upvotes: 4

izzyu
izzyu

Reputation: 36

the difference between these two methods is about usage.

You use the "File System" service to work on your local/cloud file system, like creating, moving or deleting file. You could use the Storage::put() method to store the uploaded file, of course, but you would have to get the file from the request either way. So you normally just use the $request->file('photo')->move($destinationPath); method as specified in http://laravel.com/docs/5.1/requests#files to move the file where you want it to be. The File system service is not meant to handle uploads itself. That is what the Request is for.

The question about where you put the files is one you have to answer yourself. The default path for storing files is storage/app. You can put them to public/uploads but it is discouraged as anyone knowing the URL can download the files. It really depends on what the file is meant to. If it is a say profile picture then it can be put in public/uploads. Is the file private then is should not be put there but instead in storage/app.

Upvotes: 1

Related Questions