Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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;
Copy link

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