如何从laravel 5.1中的公用文件夹中删除文件

12 laravel

我想从数据库中删除新闻,当我点击删除按钮时,数据库中的所有数据都被删除,但图像仍保留在上传文件夹中.那么,我该如何工作呢.谢谢


这是我的功能,但不删除公共目录的图像/新闻文件夹中的图像>

 public function destroy($id) {
    $news = News::findOrFail($id);
    $image_path = app_path("images/news/{$news->photo}");

    if (File::exists($image_path)) {
        //File::delete($image_path);
        unlink($image_path);
    }
    $news->delete();
    return redirect('admin/dashboard')->with('message','??? ??????? ???  ??');
}
Run Code Online (Sandbox Code Playgroud)

小智 34

您可以像@Khan建议的那样使用PHP的unlink()方法.

但是如果你想以Laravel方式实现它,请使用File :: delete()方法.

//删除单个文件

File::delete($filename);
Run Code Online (Sandbox Code Playgroud)

//删除多个文件

File::delete($file1, $file2, $file3);
Run Code Online (Sandbox Code Playgroud)

//删除文件数组

$files = array($file1, $file2);
File::delete($files);
Run Code Online (Sandbox Code Playgroud)

  • `use Illuminate\Support\Facades\File;` - 您可以将其添加到您的答案中@moris-io (7认同)
  • 通过更改路径来解决:$ image_path = public_path().'/'.$ news-> photo; 取消链接($ IMAGE_PATH); (3认同)

Kha*_*ukh 20

使用php 的unlink函数,只需将确切的路径传递给你的文件即可取消链接功能:

unlink($file_path);
Run Code Online (Sandbox Code Playgroud)

如果文件未存储在数据库中,请不要忘记创建文件的完整路径.例如

$file_path = app_path().'/images/news/'.$news->photo;
Run Code Online (Sandbox Code Playgroud)


小智 12

这在 Laravel 8 上有效

use File;

if (File::exists(public_path('uploads/csv/img.png'))) {
     File::delete(public_path('uploads/csv/img.png'));
 }
Run Code Online (Sandbox Code Playgroud)


J H*_* Ha 10

这个方法适用于我
首先,将下面的行放在控制器的开头:

use File;
Run Code Online (Sandbox Code Playgroud)

你的php文件中名称空间下面第二个:

 $destinationPath = 'your_path';
 File::delete($destinationPath.'/your_file');
Run Code Online (Sandbox Code Playgroud)

$ destinationPath - >文件夹public里面的文件夹.


小智 9

Laravel 8.x 的更新工作:

例如删除图像 ->

首先在控制器顶部添加 File Facade:

use Illuminate\Support\Facades\File;
Run Code Online (Sandbox Code Playgroud)

然后使用删除功能。如果文件位于“public/”中,则必须使用 public_path() 函数指定路径:

File::delete(public_path("images/filename.png"));
Run Code Online (Sandbox Code Playgroud)


小智 6

首先,您应该这样config/filesystems.php设置'root' => public_path()

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => public_path(),
],
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用 Storage::delete($filename);


Ogb*_*lis 6

使用PHPunlink()函数,将文件删除

$path = public_path()."/uploads/".$from_db->image_name;
unlink($path);
Run Code Online (Sandbox Code Playgroud)

以上将删除$from_db->image_name位于public/uploads文件夹中返回的图像


小智 5

尝试使用:

unlink('.'.Storage::url($news->photo));
Run Code Online (Sandbox Code Playgroud)

在调用门面存储之前查看连接


小智 5

public function destroy($id) {\n    $news = News::findOrFail($id);\n    $image_path = app_path("images/news/".$news->photo);\n\n    if(file_exists($image_path)){\n        //File::delete($image_path);\n        File::delete( $image_path);\n    }\n    $news->delete();\n    return redirect(\'admin/dashboard\')->with(\'message\',\'\xd8\xae\xd8\xa8\xd8\xb1 \xd9\x85\xd9\x88\xd9\x81\xd9\x82\xd8\xa7\xd9\x86\xd9\x87 \xd8\xad\xd8\xb0\xd9\x81  \xd8\xb4\xd8\xaf\');\n}\n
Run Code Online (Sandbox Code Playgroud)\n


小智 5

首先通过构建路径确保文件存在

if($request->hasFile('image')){
    $path = storage_path().'/app/public/YOUR_FOLDER/'.$db->image;
      if(File::exists($path)){
          unlink($path);
      }
Run Code Online (Sandbox Code Playgroud)