php - 拉维尔 5.4 : how to delete a file stored in storage/app

标签 php laravel laravel-5.3

我想删除存储在 storage/app/myfolder/file.jpg 中的文件。我尝试了以下代码,但均无效:

use File    
$file_path = url().'/storage/app/jobseekers_cvs/'.$filename;
unlink($file_path);

use File
$file_path = public_path().'/storage/app/myfolder/'.$filename;
unlink($file_path);

use File
$file_path = app_path().'/storage/app/myfolder/'.$filename;
unlink($file_path);

还有,

File::Delete('/storage/app/myfolder/'.$filename);

请帮忙。

最佳答案

你可以像这样使用 Laravels facade Storage:

Storage::delete($file);

或者你可以使用这个:

unlink(storage_path('app/folder/'.$file));

如果你想删除一个目录,你可以使用这个:

rmdir(storage_path('app/folder/'.$folder);

要提到的一个重要部分是您应该首先检查文件或目录是否存在。

所以如果你想删除一个文件,你应该这样做:

if(is_file($file))
{
    // 1. possibility
    Storage::delete($file);
    // 2. possibility
    unlink(storage_path('app/folder/'.$file));
}
else
{
    echo "File does not exist";
}

如果你想检查它是否是一个目录,这样做:

if(is_dir($file))
{
    // 1. possibility
    Storage::delete($folder);
    // 2. possibility
    unlink(storage_path('app/folder/'.$folder));
    // 3. possibility
    rmdir(storage_path('app/folder/'.$folder));
}
else
{
    echo "Directory does not exist";
}

关于php - 拉维尔 5.4 : how to delete a file stored in storage/app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45065039/

相关文章:

php - (Laravel)只检索有关系的用户

php - 如何使用 MySQL 查询选择 WordPress 中的所有类别?

java - 论坛设计的混合内容警告

php - 创建一个私有(private)包?

php - Laravel PHP 7.3 pdo_mysql 报告缺少驱动程序,可能是由于 Ubuntu 上 undefined symbol

laravel - 了解 Laravel 中的路由

php - 使用php直接上传到带有进度条的s3

php - 有没有办法让html表单消失?

php - 在 Laravel 5.3 中找不到特征 'Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers'

php - 如何在 foreach 迁移表中使用计数器? (拉拉维尔 5.3)