How to copy files from directory to another location (Laravel 8)

I want to copy my directory from one place to another

Try to use facade Storage

Storage::disk('local')->copy('path/from/', 'path/to/');

Error:

copy(): The first argument to copy() function cannot be a directory

I tried to find answer in the Internet, but didn't find for Laravel 8


Ответы (1 шт):

Автор решения: IndianCoding

Через Storage, наверное, как-то так:

$files = Storage::disk('local')->allFiles('path/from/');

foreach ($files as $file) {
    $newPath = str_replace('path/from/', 'path/to/', $file);
    Storage::disk('local')->copy($file, $newPath);
}

Ещё у File есть метод copyDirectory(), но он работает только через полные пути.

\File::copyDirectory(storage_path('app/path/from'), storage_path('app/path/to'));
→ Ссылка