【问题标题】:How can I copy a folder to another folder with Laravel?如何使用 Laravel 将文件夹复制到另一个文件夹?
【发布时间】:2020-12-28 11:32:59
【问题描述】:

正如标题所说,我正在努力将一个文件夹复制到另一个文件夹

我试过了,但没有结果

编辑:我通过修复 $npath 变量来实现这一点,我只是给出了没有文件夹名称的 url $npath = $newk->url;

所以最终的代码是:

    public function copyFolder(Request $request)
{
    $id= $request->get('oldf');
    $new = $request->get('newf');

    $document = Document::find($id);
    $newk = Document::find($new);

    $npath = $newk->url.'/'.$document->nom;

    $file= new Filesystem();

    if($file->copyDirectory($document->url, $npath)){
        return redirect('home');
    }
    return redirect('home');

}

任何帮助将不胜感激:)

【问题讨论】:

  • 您是否已在文件系统中验证该文件夹实际上并未被复制?无论操作是否成功,您的函数中的结果都是相同的。
  • 我验证了,但什么也没发生

标签: php laravel laravel-storage laravel-filesystem


【解决方案1】:

使用此功能

function recursive_files_copy($source_dir, $destination_dir)
{
    // Open the source folder / directory 
    $dir = opendir($source_dir);

    // Create a destination folder / directory if not exist 
    if (!is_dir($destination_dir)){
        mkdir($destination_dir);
    }
    // Loop through the files in source directory 
    while($file = readdir($dir))
    {
        // Skip . and .. 
        if(($file != '.') && ($file != '..'))
        {
            // Check if it's folder / directory or file 
            if(is_dir($source_dir.'/'.$file))
            {
                // Recursively calling this function for sub directory  
                recursive_files_copy($source_dir.'/'.$file, $destination_dir.'/'.$file);
            }
            else
            {
                // Copying the files
                copy($source_dir.'/'.$file, $destination_dir.'/'.$file);
            }
        }
    }
    
    closedir($dir);
}

【讨论】:

  • File::copyDirectory 已经copies recursively
  • 我应该把它放在Controller还是放在哪里?
  • @Dan 我也尝试仅使用“”手动提供路径,但根本没有结果
猜你喜欢
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 2013-05-21
相关资源
最近更新 更多