如何在Laravel 5中将一个文件夹从一个目录复制到另一个目录?

5
也许这很简单,但我在谷歌或这里找不到合适的答案!我使用了

\File::copyDirectory('public/' . $token . '/cover/', $folderName . '/cover/');

但是它们没有区别!!

这是我找到上面解决方案的地方。

您知道如何使用laravel 5.4将整个目录从一个文件夹复制到另一个文件夹吗?

提前感谢


尝试传递绝对路径而不是相对路径。也许当您执行代码时,基本目录并非看起来那么简单。(我建议在public前面加上“/”符号。) - Ohgodwhy
@Kiyarash,您能否批准我的答案? - Bas
6个回答

15

2
链接的帮助页面中没有5.6版本的该函数?更奇怪的是,在Laravel的整个文档中搜索“copyDirectory”,在8和9中都没有结果。有人知道为什么文件门面和许多函数似乎完全缺失于文档中吗?当它们仍然存在于源代码中时,这非常令人困惑。 - lowkey

8

我们可以在laravel 5.8中使用文件门面来实现,具体操作如下:

use Illuminate\Support\Facades\File;  

File::copyDirectory(__DIR__.'/form-directory', resource_path('to-directory'));

希望这对你有所帮助。


1

只需要这样做:

   /* Move test images from public folder to storage folder */
    $source = "source/path";
    $destination = "public/files";

    File::copyDirectory(base_path($source), base_path($destination));

0

我使用了存储门面并且运行良好

config/filesystems.php

'disks' => [
    'local' => [
        'driver' => 'local',
        'root' => public_path(),
    ],
     ...
]

将所有文件从public/seed_images/复制到public/images/

app/Http/Controllers/HomeController.php

use Illuminate\Support\Facades\Storage;
$fromFiles = Storage::allFiles('seed_images');
for ($i=0; $i < count($fromFiles) ; $i++) {
    $toFile = str_replace("seed_images","images",$fromFiles[$i]);
    Storage::copy( $fromFiles[$i], $toFile );
}

0
/**
 * If the destination directory does not actually exist, we will go 
 * ahead and create it recursively, which just gets the destination 
 * prepared to copy the files over. Once we make the directory we'll 
 * proceed the copying.
 *
 * function replaces files with same names
 * 
 * @var bool
 */
$result = File::copyDirectory(
    base_path() . '/storage/source',
    base_path() . '/storage/destination'
);

-3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接