Created
September 19, 2018 21:42
-
-
Save nullthoughts/ef2f9e8a81dfa8aa37b0945f4f277e8b to your computer and use it in GitHub Desktop.
Batch rename/move files with callback function
This file contains hidden or 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
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$file != $newName
on line 16 is redundant, however if caching is not utilized it could save some time on large batches