fsdolphin's avatar

Understanding the move and the storeAs methods for saving/uploading files to the filesystem

This is my first time uploading files and I have been watching videos and some online tutorials and I noticed that there are a few ways to do the upload which is a little overwhelming by not understanding the use cases.

I have seen people using the move() method, some others use the storeAs() method, some others using Storage::disk() and some other using request()->file()->store(), anyways for now I would like to focus on the move() and the storeAs() methods.

Can someone give me a quick explanation on when would you use one over the other?

Illuminate/Http/UploadedFile

storeAs()

// Store the uploaded file on a filesystem disk.

Illuminate/Filesystem/Filesystem

move()

// Move a file to a new location.
0 likes
8 replies
trcraig's avatar
trcraig
Best Answer
Level 5

store() stores a file with a hash of the file name vs storeAs() allows you to control the file name. Both are on the UploadedFile object (which lives in your /tmp folder or wherever PHP tells temporary files to live). Using these methods takes a file from its temporary location and stores it in your App's "storage" system of choice (by default, the storage/app folder)

move() is for existing files within your Storage system to move from wherever it is now to wherever you want it, preserving the file name.

So, use storeAs() or store() when you are working with a file that has been uploaded (i.e. within a controller), and move() only when you've already got a file in the system to move it from one location to another.

I hope this helps. (Sorry for the man updates, trying to make it clear as possible)

6 likes
fsdolphin's avatar

@trcraig I also watched that video but for some reason I couldn't make it to save to the public directory so I assumed it was a helper for private uploads since it saves to the storage folder by default.

Quick question, do you how can I make it to save to the public directory? I rather use this method.

Route::post('routeName', function(){
    request()->file('fieldName')->store('public/images');
    return back();
});

https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/12

trcraig's avatar

I haven't done this, but I believe you can do something such as:

Route::post('routeName', function($request) {
    $file = $request->file('fieldName');
    $file->storePublicly('images');
});

See: \Illuminate\Http\UploadedFile::storePublicly()

1 like
fsdolphin's avatar

I tried your code and it saves to the storage folder. I guess I will stick with the move method.

Route::post('routeName', function($request) {
        $file = $request->file('fieldName');
        $file->storePublicly('images');
});
Snapey's avatar

it's all explained in the docs.

https://laravel.com/docs/5.4/filesystem

store will use your default disk, which you can set to be other than the storage folder, or, you can create a new disk in config and then specify that as the second parameter for the store command.

Please or to participate in this conversation.