Black Friday: coupon FRIDAY24 for 40% off Yearly/Lifetime membership! Read more here

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.

// List all the files from a folder
$files = Storage::disk('origin')->allFiles('/from-folder-name');
 
// Using normal get and put (the whole file string at once)
foreach($files as $file) {
Storage::disk('destination')->put(
"optional-folder-name" . basename($file),
Storage::disk('origin')->get($file)
);
}
 
// Best: using Streams to keep memory usage low (good for large files)
foreach ($files as $file) {
Storage::disk('destination')->writeStream(
"optional-folder-name" . basename($file),
Storage::disk('origin')->readStream($file)
);
}

Tip given by @alanrezende

Like our articles?

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

Recent New Courses