0

I wanna copy or move a directory to a new directory in laravel but the copyDirectory function always return false to me. Am I doing anything wrong with my code? Thanks in advance

$destinationDir = 'storage/Components/'.'Compressor'.'/'.$approvedRequest['compressor_model'].'/';
$success = File::copyDirectory(public_path().$approvedRequest['attachment_dir'], public_path().$destinationDir,true);
$compressorUpdate->attachment_dir = $destinationDir;
Hanyi Koh
  • 327
  • 1
  • 4
  • 15

2 Answers2

0

I believe current directory is in

storage/Components/Compressor/{$approvedRequest['compressor_model']}/

and want to copy to

public/storage/Components/Compressor/{$approvedRequest['compressor_model']}/

so you can simplify

$path="Components/Compressor/{$approvedRequest['compressor_model']}/";

$currentDirectory=storage_path($path);
$destinationDir =public_path($path);

$success = File::copyDirectory($currentDirectory,$destinationDir);
John Lobo
  • 14,355
  • 2
  • 10
  • 20
0

I think you're trying to copy to a symlinked directory. You should refer to the realpath of the directory instead of its symlink. So use storage_path() instead.

$destinationDir = implode(DIRECTORY_SEPARATOR, ['Components', 'Compressor', $approvedRequest['compressor_model']]);
        $success = \File::copyDirectory(
            public_path() . $approvedRequest['attachment_dir'],
            storage_path() . DIRECTORY_SEPARATOR . $destinationDir, true);
        $compressorUpdate->attachment_dir = $destinationDir;
SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32