Laravel 5.1 文件未从文件夹中删除
Laravel 5.1 File not deleting from folder
我想从我的数据库和 public/uploads 文件夹中删除一个 pdf 文件。它正在从数据库中删除,而不是从我的 public 文件夹中删除。
这是我的控制器:
public function deleteArticle($id) {
$article = Article::findOrFail($id);
File::delete($article->document);
$article->delete();
return redirect()->back();
}
/*This handles the posting of the file into the folder and storing of the url into the datab
$file = Input::file('document');
$file->move('uploads', $file->getClientOriginalName());
$document = asset('uploads/'.$file->getClientOriginalName());
$newArticle->document = $document;
由于您当前正在将 url 保存到数据库(通过使用 asset() 函数),您无法使用该信息删除文件。
通常只在数据库中保存文档名称就足够了。
$document = $file->getClientOriginalName();
$newArticle->document = $document;
要删除文件,您可以调用:
File::delete(public_path('uploads/'.$article->document);
要Link到你的文件你可以使用asset()方法
asset('uploads/'.$article->document);
将完整的 URL 存储在数据库中不是一个好主意。以后维护文件会很困难。最好的方法是只存储带扩展名的文件名。
如果数据库中只有文件名,可以这样删除文件:
$article = Article:findOrFail($id);
$document = $article->document; // take the image name from database
File::delete('uploads/'.$document); // delete the file
$article->delete() // delete the record from database
编辑
如果您仍想在数据库中使用 URL,您可以使用 substr()
和 strpos()
函数获取图像名称。示例:
$image = substr($article->document,0,strpos("uploads/"));
您只能从 URL 中获取文档名称并使用它来删除文件。
要仅存储文件名,请遵循以下步骤:
$document = $request->file('document')->getClientOriginalName();
我想从我的数据库和 public/uploads 文件夹中删除一个 pdf 文件。它正在从数据库中删除,而不是从我的 public 文件夹中删除。
这是我的控制器:
public function deleteArticle($id) {
$article = Article::findOrFail($id);
File::delete($article->document);
$article->delete();
return redirect()->back();
}
/*This handles the posting of the file into the folder and storing of the url into the datab
$file = Input::file('document');
$file->move('uploads', $file->getClientOriginalName());
$document = asset('uploads/'.$file->getClientOriginalName());
$newArticle->document = $document;
由于您当前正在将 url 保存到数据库(通过使用 asset() 函数),您无法使用该信息删除文件。
通常只在数据库中保存文档名称就足够了。
$document = $file->getClientOriginalName();
$newArticle->document = $document;
要删除文件,您可以调用:
File::delete(public_path('uploads/'.$article->document);
要Link到你的文件你可以使用asset()方法
asset('uploads/'.$article->document);
将完整的 URL 存储在数据库中不是一个好主意。以后维护文件会很困难。最好的方法是只存储带扩展名的文件名。
如果数据库中只有文件名,可以这样删除文件:
$article = Article:findOrFail($id);
$document = $article->document; // take the image name from database
File::delete('uploads/'.$document); // delete the file
$article->delete() // delete the record from database
编辑
如果您仍想在数据库中使用 URL,您可以使用 substr()
和 strpos()
函数获取图像名称。示例:
$image = substr($article->document,0,strpos("uploads/"));
您只能从 URL 中获取文档名称并使用它来删除文件。
要仅存储文件名,请遵循以下步骤:
$document = $request->file('document')->getClientOriginalName();