php Laravel 5.4:如何删除存储在存储/应用程序中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45065039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:04:46  来源:igfitidea点击:

Laravel 5.4: how to delete a file stored in storage/app

phplaravellaravel-5.3

提问by Ajmal Razeel

I want to delete a file that is stored in storage/app/myfolder/file.jpg. I have tried the following codes but none of this works:

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

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

and

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

and

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

and also,

并且,

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

Please help.

请帮忙。

回答by utdev

You could either user Laravels facade Storagelike this:

你可以Storage像这样使用 Laravel 外观:

Storage::delete($file);

or you could use this:

或者你可以使用这个:

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

If you want to delete a directory you could use this:

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

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

One important part to mention is that you should first check wether the file or directory exists or not.

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

So if you want to delete a file you should probably do this:

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

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

And if you want to check wether it is a directory do this:

如果你想检查它是否是一个目录,请执行以下操作:

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";
}

回答by Minar Mnr

Use storage

使用存储

//demo 
use Illuminate\Support\Facades\Storage;

Storage::delete($filename);

Another way,

其它的办法,

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

回答by Ajmal Razeel

I found the answer. This code worked for me.

我找到了答案。这段代码对我有用。

unlink(storage_path('app/foldername/'.$filename));

回答by Mateus Gon?alves

The delete method accepts a single filename or an array of files to remove from the disk:

delete 方法接受要从磁盘中删除的单个文件名或文件数组:

use Illuminate\Support\Facades\Storage;

Storage::delete('file.jpg');

Storage::delete(['file.jpg', 'file2.jpg']);

If necessary, you may specify the disk that the file should be deleted from:

如有必要,您可以指定应从中删除文件的磁盘:

use Illuminate\Support\Facades\Storage;

Storage::disk('s3')->delete('folder_path/file_name.jpg');

Delete A Directory

删除目录

Finally, the deleteDirectory method may be used to remove a directory and all of its files:

最后,deleteDirectory 方法可用于删除目录及其所有文件:

Storage::deleteDirectory($directory);

回答by Jannie Theunissen

The default root for the Storage facade is configured in config/filesystems.php and the default is:

Storage facade 的默认根在 config/filesystems.php 中配置,默认值为:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

So you should use:

所以你应该使用:

Storage::delete('myfolder/'.$filename)

回答by jamel nefzi

This code worked for me.

这段代码对我有用。

use Illuminate\Support\Facades\Storage;
....
$storagePath  = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
if(file_exists($storagePath.$file)) unlink($storagePath.$file);