我尝试了一堆方法来删除图像,但是laravel出于某种原因只是忽略了删除部分。我试图查看它是否获得了与dd($filename)
的链接,并且它获得了正确的链接。我也尝试过php
unlink
,但在将path
正确通过的问题上遇到了问题
Storage::delete('storage/images/'.$filename);
这就是我尝试过的,laravel表现得很像
答案 0 :(得分:1)
可能是路径问题或文件许可问题:
$exists = Storage::exists('storage/images/'.$filename);
if($exists){
Storage::delete('storage/images/'.$filename);
//file should be deleted here.... if not check permissions in storage path
//webserver sys user should have +r+w on storage path
}else{
echo "file not found in ".storage_path('storage/images/').$filename;
}
答案 1 :(得分:0)
您应该提供更多信息。检查您是否在做
use Illuminate\Support\Facades\Storage;
之前
Storage::delete('storage/images/'.$filename);
也许您有一些错误或其他调试信息?
答案 2 :(得分:0)
添加此代码以使用存储空间:
use Illuminate\Support\Facades\Storage;
然后通过以下代码查找文件:
$file = Storage::exists('storage/images/'.$filename);
然后删除文件(如果存在)
if ($file) {Storage::delete('storage/images/'.$filename)}
else {
return "File Not exist";
}
N:B确保要删除的文件具有适当的权限
答案 3 :(得分:0)
首先找到有关特定ID的数据 例如
$post = \App\Post::find($id);
unlink(public_path().'/uploads/post/'.$post->image);
$post->delete();
答案 4 :(得分:0)
我最终使用了原始的PHP方法来做到这一点。
unlink('storage/images/'.$filename);
unlink
运行良好,不需要任何设置即可完成。