从Laravel中的文件夹复制文件

时间:2019-04-05 19:32:09

标签: php laravel file

我正在与laravel合作开发CMS,以防万一我发布品牌网站的页面,需要在发布之前创建文件夹结构。

如果我有 app / themes / default / public我想复制public文件夹中的文件,并将它们发送到将要创建的该文件夹app / websites-published / '. $ content-> nanoid

我该怎么做?

    public function publish(Request $request, Site $site)
    {
        /** @var  $contents */
        $contents = $site->contents;

        /** @var  $content */
        foreach ($contents as $content) {

            // Copiar o Public do THEME - THEME/NOME DO TEMA/PUBLIC - WebsitePublished->Nanoid
            // Conteudo do ex: THEME/PUBLIC
            $copyTheme = storage_path('app/themes/default/public');
            $sendTheme = public_path('app/websites-published/' . $content->nanoid);
            copy($copyTheme, $sendTheme);


            // Copiar o Public do WEBSITE - WebsitePublished->Site:Nanoid
            // Conteudo do ex: WEBSITE/SUZUKI/PUBLIC


            // APP/WEBPUBLISHED
        }

        return $this->viewService->renderContent($request, $content);
    }

1 个答案:

答案 0 :(得分:1)

Laravel有一个文件系统,可以处理大多数系统上的文件操作。 Storage门面类只能复制单个文件,因此您需要轻松地循环访问所有文件,但这太费力了,因为类Filesystem可以在创建目录时递归地复制或移动整个目录。目录递归。

要移动整个目录,您必须导入Filesystem类:

use Illuminate\Filesystem\Filesystem;

并使用copyDirectory()方法:

public function publish(Request $request, Site $site)
{
    /** @var  $contents */
    $contents = $site->contents;

    /** @var  $content */
    foreach ($contents as $content) {

        // Copiar o Public do THEME - THEME/NOME DO TEMA/PUBLIC - WebsitePublished->Nanoid
        // Conteudo do ex: THEME/PUBLIC
        $copyTheme = storage_path('app/themes/default/public');
        $sendTheme = public_path('app/websites-published/' . $content->nanoid);
        $filesys = new Filesystem();
        $filesys->moveDirectory($copyTheme, $sendTheme);


        // Copiar o Public do WEBSITE - WebsitePublished->Site:Nanoid
        // Conteudo do ex: WEBSITE/SUZUKI/PUBLIC


        // APP/WEBPUBLISHED
    }

    return $this->viewService->renderContent($request, $content);
}