Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stwilson's avatar

How to move all files in directory from one filesystem to another?

My storage is set like so:

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'remote' => [
            'driver' => 'local',
            'root'   => '//freenas/remote_storage',
        ]

How do I move all files in a directory on 'local' to 'remote'?

I see League\Flysystem\MountManager and something like this should be possible:

$mountManager->move('local://somepath', 'remote://somepath');

MountManager calls for a League\Flysystem\FilesystemInterface and Laravel storage adapters are type Illuminate\Filesystem\FilesystemAdapter so I didn't see a connection there.

What is the Laravel way to move or copy files or directories from one file system to another? Unless I overlooked it, the documentation shows only this: Storage::move('old/file1.jpg', 'new/file1.jpg');.

0 likes
3 replies
TerrePorter's avatar
Level 12

you need to have the full path to the file not the disk:path.

try something like...

// convert to full pathss
        $full_path_source = Storage::disk($sourceDisk)->getDriver()->getAdapter()->applyPathPrefix($sourceFile);
        $full_path_dest = Storage::disk($destDisk)->getDriver()->getAdapter()->applyPathPrefix($destFile);


// make destination folder
        if (!File::exists(dirname($full_path_dest))) {
            File::makeDirectory(dirname($full_path_dest), null, true);
        }

        File::move($full_path_source, $full_path_dest);
stwilson's avatar

@TerrePorter Thanks. This did the job:

$files = Storage::files('temp/unzip');
foreach ($files as $file) {
    $full_path_source = Storage::getDriver()->getAdapter()->applyPathPrefix($file);
    $full_path_dest = Storage::disk('remote')->getDriver()->getAdapter()
            ->applyPathPrefix($saveFolder . '/' . basename($file));
    File::move($full_path_source, $full_path_dest);
}
2 likes
KiddTang's avatar

This is cleaner... and sub-folders are created automatically.

use League\Flysystem\MountManager;

$mountManager = new MountManager([
    's3' => \Storage::disk('s3')->getDriver(),
    'local' => \Storage::disk('local')->getDriver(),
]);
$mountManager->copy('s3://path/to/file.txt', 'local://path/to/output/file.txt');

Source: https://stackoverflow.com/a/46188165/9104189

2 likes

Please or to participate in this conversation.