Using "put" Method to Save the File to the New Disk
For this example, we'll be moving files located from the "public" disk to the "s3" disk.
<?php Storage::disk('public')->put( 'new-file-1.jpg', Storage::disk('public')->get('old-file-1.jpg') );
Another Approach To Move File To Different Disk in Laravel
Another approach to move the file to a different disk is to get the directory path for both disks and then use the "File::move()" method to transfer the files. Do refer the code example below for the full code example.
<?php $sourceFile = ''; $destFile = ''; $sourceDisk = 'public'; $destDisk = 's3'; // convert to full paths $pathSource = Storage::disk($sourceDisk)->getDriver()->getAdapter()->applyPathPrefix($sourceFile); $destinationPath = Storage::disk($destDisk)->getDriver()->getAdapter()->applyPathPrefix($destFile); // make destination folder if (!File::exists(dirname($destinationPath))) { File::makeDirectory(dirname($destinationPath), null, true); } File::move($pathSource, $destinationPath);
Leave a reply