php 如何从 Laravel 5 中的 Public/Images 文件夹中删除图像(URL 数据)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44710894/
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
How to Delete Images from Public/Images Folder in laravel 5 (URL Data)
提问by DarkCode999
how to delete images file from public/images folder in laravel 5 ??
如何从laravel 5 的public/images 文件夹中删除图像文件?
i found some example from this site, but i know they are just using the file name in their record table
, but i'm using something like URL
e.g localhost/project/uploads/filename.jpg
on my record table
. so if i tried like this :
我发现了一些例子,从这个网站,但我知道他们只是在他们使用的文件名record table
,但我使用类似URL
如localhost/project/uploads/filename.jpg
我record table
。所以如果我这样尝试:
$image_path = $data->image; // the value is : localhost/project/image/filename.format
if(File::exists($image_path)) {
File::delete($image_path);
}
the file is not deleted
文件没有被删除
help pls, thanks
请帮忙,谢谢
回答by Sovary
If you want to delete image from your server, you have to reference location of file in directory server, means you could not reference by url link to delete it.
如果你想从你的服务器上删除图像,你必须在目录服务器中引用文件的位置,这意味着你不能通过 url 链接引用来删除它。
Commonly, Laravel 5 file is locate in publicfolder.
通常,Laravel 5 文件位于公共文件夹中。
Example: your files are located in public/images
示例:您的文件位于 public/images
$image_path = "/images/filename.ext"; // Value is not URL but directory file path
if(File::exists($image_path)) {
File::delete($image_path);
}
If I can delete image from server by reference URL then Google is the first target :)
如果我可以通过引用 URL 从服务器中删除图像,那么 Google 是第一个目标:)
回答by TheLastCodeBender
You can use the normal PHP delete file keyword ( @unlink )
您可以使用普通的 PHP 删除文件关键字(@unlink)
$image_path = "the name of your image path here/".$request->Image;
if (file_exists($image_path)) {
@unlink($image_path);
}
回答by Edward
I had the same problem today but now the files are deleting by this code
我今天遇到了同样的问题,但现在文件正在被这段代码删除
$image_path = $data->image;
if(file_exists($image_path)) {
File::delete($image_path);
}
basically the poblem for me was the if condition didn't returned somthing true. and the statement didnt executed.
对我来说,问题基本上是 if 条件没有返回 true 。并且该语句没有执行。
btw I'm storing my file in public folder in laravel
顺便说一句,我将我的文件存储在 Laravel 的公共文件夹中
hope this helps someone.
希望这有助于某人。
回答by khokon ahmed
public function destroy($id)
{
$imagePath = YourModelName::select('image')->where('id', $id)->first();
$filePath = $imagePath->image;
if (file_exists($filePath)) {
unlink($filePath);
YourModelName::where('id', $id)->delete();
}else{
YourModelName::where('id', $id)->delete();
}
}
回答by harshal sathavara
call this function and pass two parameter $filepath = path where your file exist $filename = name of your file
调用此函数并传递两个参数 $filepath = 文件所在的路径 $filename = 文件名
public static function UnlinkImage($filepath,$fileName)
{
$old_image = $filepath.$fileName;
if (file_exists($old_image)) {
@unlink($old_image);
}
}