嗨,我想从数据库及其文件夹public/files
删除文件我已经写了一些代码,但这只是从数据库中删除,而不是从文件夹中删除
代码:
public function destroy($id)
{
$file = File::find($id);
$file->delete(public_path('files'));
$file->delete();
return redirect('file')->with('success', 'Data is successfully deleted');
}
my db table:
Schema::create('files', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('filename');
$table->timestamps();
});
操作方法
答案 0 :(得分:0)
public function destroy($id)
{
$file = File::find($id);
unlink(public_path('file'));
$file->delete();
return redirect('file')->with('success', 'Data is successfully deleted');
}
答案 1 :(得分:0)
使用Unlink()
函数从目录中删除,并使用DB查询从数据库中删除。
public function destroy($id)
{
$file = File::find($id);
$file_path = $file->path; //Please use that variable ehich you are using to save file path.
unlink($file_path); //it will looks like file/path/image.png
$file->delete();
return redirect('file')->with('success', 'Data is successfully deleted');
}
希望它会起作用。谢谢!
答案 2 :(得分:0)
PHP unlink
函数用于从目录中删除文件
第1步:首先,您需要从数据库中获取文件路径。
第2步:使用unlink('path_name')从目录中删除文件
第3步:从数据库中删除行
public function destroy($id){
$FilePath=DB::table('files')->select('filename')->where('id',$id)->first();
$path = public_path()."/uploads/".$FilePath->filename;
unlink($path); // Delete From Folder
$result=DB::table('files')->where('id',$id)->delete(); // Delete From DB
return redirect('file')->with('success', 'Data is successfully deleted');
}
更新
unlink
是一个PHP函数,以Laravel方式使用File::delete() .
示例:File::delete($filename);
文件中必须包含use File;