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

romulo27's avatar

How to copy Files and Folders in laravel

In laravel as I do to copy the files from one folder to another. In case I have the following folder.

app/themes/default/public and I want to copy all the files from the public folder and put in that directory,

app/websites-published/'. $ content-> nanoid

How do I do this?

0 likes
7 replies
Snapey's avatar

From the docs

Storage::move('old/file.jpg', 'new/file.jpg');

I think you probably have to iterate - not sure if move supports wildcards - give it a go?

2 likes
Snapey's avatar

err

 Storage::copy('old/file.jpg', 'new/file.jpg');
redhedded1's avatar
// get source directory
$pathSource = Storage::disk('images')->getDriver()->getAdapter()->applyPathPrefix(null);

// get destination directory (already exists)
$pathDestination = Storage::disk('imagesBackup')->getDriver()->getAdapter()->applyPathPrefix(null);

// copy all the files from source to destination directories
File::copyDirectory($pathSource, $pathDestination);


// if doing this routinely, as in some job you can clear out the destination first: 
$oldBackup = Storage::disk('imagesBackup')->allFiles();
 if ($oldBackupImages) {
      Storage::disk('imagesBackup')->delete($oldBackupImages);
 }

// config file
...
'disks' => [
    'images' => [
            'driver' => 'local',
            'root' => public_path(assets/itemImages/'),
            'url' => env('APP_URL').'/assets/itemImages/',
            'visibility' => 'public',
        ],

  'imagesBackup' => [
            'driver' => 'local',
            'root' => public_path('assets/backupOfImages/'),
        ],	
...
redhedded1's avatar

I have an error in the variable name here, it should be:

// if doing this routinely, as in some job you can clear out the destination first: 
$oldBackupImages = Storage::disk('imagesBackup')->allFiles();
 if ($oldBackupImages) {
      Storage::disk('imagesBackup')->delete($oldBackupImages);
 }
Artwork's avatar

I am sorry, but to clarify, was the code generated using an effortless and sorrowful LLM? If so, it makes sense it's invalid - there's no such method in the general local file-system driver (Laravel v10.38.1):

> Storage::disk('local')->getDriver()->getAdapter();

   Error  Call to undefined method League\Flysystem\Filesystem::getAdapter().

Otherwise, please specify the driver you used.

Please or to participate in this conversation.