laravel 拉拉维尔。unlink('文件路径'):权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46373597/
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
Laravel. unlink ('file path'): Permission Denied
提问by Paolo Vedorin
I'm new in Laravel. I was trying to delete some files from public folder of my project. Defined 'delete' function as follow
我是 Laravel 的新手。我试图从我的项目的公共文件夹中删除一些文件。定义的“删除”功能如下
public function delete($id)
{
$files = File::find($id);
$filedelete = $files['name'];
Storage::delete('public/upload/',$filedelete);
$files->delete();
return back();
}
when I press delete input in my view
当我在视图中按下删除输入时
<a href="{{ action('FileController@delete',$link['id']) }}" class="btn btn warning">
<button id="elimina" class="btn btn-danger" type="submit">Elimina</button>
</a>
with this route in web.php
在 web.php 中使用此路由
Route::get('/showFile/{id}/delete',['as'=>'delete', 'uses'=>'FileController@delete']);
it gives me this error message
它给了我这个错误信息
unlink(C:\wamp64\www\laravel\[nameproject]\storage\app\public/upload): Permission denied
(Trying) to be as clear as possible, I upload a file in a database and in this folder. $id is the primary key of the table where there is the reference of this file. I think my problem is with $files['name'] because if I write the exact name of the file it works. But I need to make this delete dynamic, for sure. I hope to be clear, I'm sorry if not.
(试图)尽可能清楚,我在数据库和这个文件夹中上传了一个文件。$id 是该文件引用所在表的主键。我认为我的问题出在 $files['name'] 上,因为如果我写下文件的确切名称,它就可以工作。但我肯定需要使这个删除动态化。我希望说清楚,如果没有,我很抱歉。
Thanks a lot in advance. All the best.
非常感谢。祝一切顺利。
PS With this line in function delete, it works
PS在函数删除中使用这一行,它可以工作
Storage::delete('public/upload/[filename]');
Edit
编辑
$files = File::find($id);
$filedelete = $files['name'];
return "".$filedelete;
it returns me the correct name of the file. So, my problem is just how to delete it from that folder?
它返回文件的正确名称。所以,我的问题是如何从该文件夹中删除它?
采纳答案by BobB
You said you stored the reference to the file in the db so this should work:
你说你在数据库中存储了对文件的引用,所以这应该可以工作:
$files = File::find($id);
Storage::delete('public/upload/'.$files->filePathColumnName);
Changed the , to a . in your delete. Would probably go with:
将 , 更改为 。在你的删除中。可能会搭配:
public_path().'/uploads/'.$files->filePathColumnName
depending on how much of the path you put in the db
取决于您在数据库中放入了多少路径
回答by wilma bullo
This error happened to me when I opened the a file:
当我打开一个文件时,这个错误发生在我身上:
$handle = @fopen($filepath, "r");
But didn't close it after reading.
但是看完后没有关闭。
To fix this I added a file close command after reading the content of the file
为了解决这个问题,我在读取文件内容后添加了一个文件关闭命令
fclose($handle);