Skip to content

Instantly share code, notes, and snippets.

@nullthoughts
Created September 19, 2018 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullthoughts/ef2f9e8a81dfa8aa37b0945f4f277e8b to your computer and use it in GitHub Desktop.
Save nullthoughts/ef2f9e8a81dfa8aa37b0945f4f277e8b to your computer and use it in GitHub Desktop.
Batch rename/move files with callback function
public static function testRename()
{
self::batchRename('s3', 'documents', function ($file) {
return str_replace('%20', '-', $file);
});
}
public static function batchRename($disk, $directory, $callback)
{
$disk = \Illuminate\Support\Facades\Storage::disk($disk);
$files = $disk->allFiles('documents');
foreach ($files as $file) {
$newName = $callback($file);
if ($file != $newName && !$disk->exists($newName)) {
$disk->move($file, $newName);
}
echo $file . ' -> ' . $newName . PHP_EOL;
}
}
@nullthoughts
Copy link
Author

nullthoughts commented Sep 20, 2018

$file != $newName on line 16 is redundant, however if caching is not utilized it could save some time on large batches

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