【问题标题】:I can't delete a directory in Laravel 9我无法删除 Laravel 9 中的目录
【发布时间】:2023-01-01 15:24:20
【问题描述】:

我如何能够删除 Laravel 9 中包含文件的目录?我使用Storage::deleteDirectory()删除目录,但它不起作用。

这是我的方法:

protected function processFile($folder, $file)
{
    $path = storage_path('app/public/tempFolder/'.$folder.'/'.$file);

    if(file_exists($path))
    {
        copy($path, public_path('storage/files/'.$file));
        Storage::deleteDirectory($path);
    }
}

我已经有了储物柜。上面的代码会将文件从$path 复制到public_path. 之后,我想删除该目录,但它不会删除,但 copy() 有效。

有人能告诉我为什么吗?

【问题讨论】:

    标签: laravel-9


    【解决方案1】:

    尝试以下操作。您需要将目录的完整路径传递给 deleteDirectory() 方法,而不仅仅是文件的路径。

    protected function processFile($folder, $file)
    {
        $path = storage_path('app/public/tempFolder/'.$folder.'/'.$file);
    
        if(file_exists($path))
        {
            copy($path, public_path('storage/files/'.$file));
    
            // Delete the directory and all its contents
            Storage::deleteDirectory(storage_path('app/public/tempFolder/'.$folder));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 2010-09-09
      • 2016-04-11
      • 1970-01-01
      • 2012-04-03
      • 2021-01-01
      相关资源
      最近更新 更多