Laravel 5.4:如何删除存储在存储/应用程序中的文件 [英] Laravel 5.4: how to delete a file stored in storage/app
本文介绍了Laravel 5.4:如何删除存储在存储/应用程序中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除存储在storage/app/myfolder/file.jpg中的文件.我已经尝试了以下代码,但是这些都不起作用:
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:
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
这样:
You could either user Laravels facade Storage
like this:
Storage::delete($file);
或者您可以使用此:
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";
}
这篇关于Laravel 5.4:如何删除存储在存储/应用程序中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文