Copy file or all files from a folder

You can use the readStream and writeStream to copy a file (or all files from a folder) from one disk to another keeping the memory usage low.

1// List all the files from a folder
2$files = Storage::disk('origin')->allFiles('/from-folder-name');
3 
4// Using normal get and put (the whole file string at once)
5foreach($files as $file) {
6 Storage::disk('destination')->put(
7 "optional-folder-name" . basename($file),
8 Storage::disk('origin')->get($file)
9 );
10}
11 
12// Best: using Streams to keep memory usage low (good for large files)
13foreach ($files as $file) {
14 Storage::disk('destination')->writeStream(
15 "optional-folder-name" . basename($file),
16 Storage::disk('origin')->readStream($file)
17 );
18}

Tip given by @alanrezende

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord