Last active
May 7, 2024 16:03
-
-
Save amir9480/e13bf46fb087a3fe12c233c9c0b484c3 to your computer and use it in GitHub Desktop.
Laravel copy all files from one storage disk to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this can do a job:
File::copyDirectory(Storage::disk('disk_name')->path('from'), Storage::disk('users_avatars')->path('to') );