Skip to content

Instantly share code, notes, and snippets.

@amir9480
Last active May 7, 2024 16:03
Show Gist options
  • Save amir9480/e13bf46fb087a3fe12c233c9c0b484c3 to your computer and use it in GitHub Desktop.
Save amir9480/e13bf46fb087a3fe12c233c9c0b484c3 to your computer and use it in GitHub Desktop.
Laravel copy all files from one storage disk to another
<?php
if (! function_exists("copy_storage")) {
/**
* Copy files from one disk to another.
*
* @param string $from from disk name
* @param string $to to disk name
* @param string $directory if you want just copy specific directory
* @return void
*/
function copy_storage($from, $to, $directory = '/')
{
foreach (Storage::disk($from)->files($directory) as $file) {
if (Storage::disk($to)->exists($file) && Storage::disk($to)->size($file) != Storage::disk($from)->size($file)) {
Storage::disk($to)->delete($file);
}
if (! Storage::disk($to)->exists($file)) {
Storage::disk($to)->writeStream($file, Storage::disk($from)->readStream($file));
echo "Copied: $file\n";
}
}
foreach (Storage::disk($from)->directories($directory) as $dir) {
copy_storage($from, $to, $dir);
}
}
}
@burasuk
Copy link

burasuk commented Apr 14, 2023

I think this can do a job:
File::copyDirectory(Storage::disk('disk_name')->path('from'), Storage::disk('users_avatars')->path('to') );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment