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:
  • 59 courses (1057 lessons, total 42 h 44 min)
  • 79 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials