Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从存储器/app/public/photos中删除Laravel文件_Laravel_File - Fatal编程技术网

从存储器/app/public/photos中删除Laravel文件

从存储器/app/public/photos中删除Laravel文件,laravel,file,Laravel,File,我正在尝试从存储器/app/public/photos中删除文件。 我从前端得到的url是http/IP\u Address/app\u name/public/storage/photos/file\u name。我尝试了unlink和File:delete,但都不起作用,因为我认为filepath不正确。我尝试了basename和public_path,但没有解决问题。有什么帮助吗 $file_path = public_path(basename($fullLinkToPhoto)); i

我正在尝试从存储器/app/public/photos中删除文件。 我从前端得到的url是http/IP\u Address/app\u name/public/storage/photos/file\u name。我尝试了unlink和File:delete,但都不起作用,因为我认为filepath不正确。我尝试了basename和public_path,但没有解决问题。有什么帮助吗

$file_path = public_path(basename($fullLinkToPhoto));
if(File::exists($file_path)) File::delete($file_path);
编辑:以下答案由larabee提供。我使用了存储方法。这是我的最终代码。我使用basename从完整的照片链接中获得了文件名,并在Storage::delete中使用了它

$file_name = basename($fullLinkToPhoto);     
if(\Storage::exists('photos/'.$file_name)){
    \Storage::delete('photos/'.$file_name);
}

有三种不同的方式:

存储/应用/公共/

按存储:

  if(\Storage::exists('photos/picture.png')){
    \Storage::delete('photos/picture.png');
  }
按文件系统:

 if(\File::exists(public_path('photos/picture.png'))){
    \File::delete(public_path('photos/picture.png'));
}
通过php

if(file_exists(public_path('photos/picture.png'))){
  unlink(public_path('photos/picture.png'));
}

您应该使用“存储解决方案”

如果您的文件存储在
storage/app/public/photos
中,请使用
storage::delete('public/photos/filename.ext')