简体   繁体   English

Laravel 5文件删除错误

[英]Laravel 5 File Delete Error

There is an issue related to my laravel 5 web application regarding the file deletion. 关于文件删除,我的laravel 5 Web应用程序存在一个问题。

I want to delete an entry from my table with corresponding image file. 我想从我的表中删除一个带有相应图像文件的条目。 Here is my table structure. 这是我的表格结构。

 Schema::create('master_brands',function($table){
        $table->increments('pk_brand_id');
        $table->string('brand_name');
        $table->string('brand_logo');
        $table->timestamps();
    });

I added 3 brands to db and storing image path in brand_logo field like : ( /uploads/masters/logocatagory_Computers & IT.png ). 我在db中添加了3个品牌,并在brand_logo字段中存储图像路径,如:( / uploads / masters / logocatagory_Computers&IT.png )。

When deleting the record from database I want to delete the image file from uploads folder also. 从数据库中删除记录时我也想从uploads文件夹中删除图像文件。 But the File was not deleting when I performing delete action. 但是当我执行删除操作时,文件没有删除。 here is my delete controller. 这是我的删除控制器。

  public function branddelete($id)
  {
    $filename = DB::table('master_brands')->where('pk_brand_id', $id)->get();
    $filname =  url()."/app".$filename[0]->brand_logo;
    File::delete($filname); // http://localhost/genie-works/devojp/app//uploads/masters/logocatagory_Computers & IT.png
  }

The file is exist in my directory and the directory having 777 permission. 该文件存在于我的目录中,并且该目录具有777权限。 !!

I also tried the Storage class to delete file.But there is no way !!!.. 我也试过Storage类来删除文件。但是没有办法!!! ..

How can I solve this issue? 我该如何解决这个问题? Any Ideas... :( 有任何想法吗... :(

You are doing it the wrong way. 你这样做是错误的。 You can't possibly delete a file from URL, you must provide file PATH not URL 您不可能从URL中删除文件,您必须提供文件PATH而不是URL

Edited 编辑

Assuming the file you wish to delete is located in public/uploads/masters/logocatagory_Computers you could do this: 假设您要删除的文件位于public/uploads/masters/logocatagory_Computers您可以这样做:

public function branddelete($id)
{
    $filename = DB::table('master_brands')->where('pk_brand_id', $id)->get();
    $filname =  $filename[0]->brand_logo; 
    //example it.png, which is located in `public/uploads/masters/logocatagory_Computers` folder
    $file_path = app_path("uploads/masters/logocatagory_Computers/{$filname}");

    if(File::exists($file_path)) File::delete($file_path); 
}

If you have the file out Storage/app/ , Storage:: does not work, change the /Config/Filesystems.php try put your 'local' at base_path() and comment default storage_path(), 如果你有文件out Storage / app /,Storage ::不起作用,请更改/Config/Filesystems.php尝试将你的'local'放在base_path()并注释默认的storage_path(),

    'disks' => [

    'local' => [
        'driver' => 'local',
        'root'   => base_path(),
        // 'root'   => storage_path('app'),
    ],

Then in Controller 然后在Controller中

use Illuminate\Support\Facades\Storage;

Then in public funtion use Storage Model 然后在公共功能中使用存储模型

$path= 'uploads/masters/';
Storage::delete($path . $filname);

In Laravel 5.1. 在Laravel 5.1中。 When using the local driver, note that all file operations are relative to the root directory defined in your configuration file. 使用本地驱动程序时,请注意所有文件操作都与配置文件中定义的根目录相关。 By default, this value is set to the storage/app directory. 默认情况下,此值设置为storage / app目录。 Therefore, 因此,

the following method would store a file in storage/app/file.txt: 以下方法将文件存储在storage / app / file.txt中:

Storage::put('file.txt', 'Contents');

this would delete file.txt in storage/app 这会删除storage / app中的file.txt

Storage::delete('file.txt');

If you want to change the root directory,take ' storage/app/uploads/ ' for example , you could set the filesystem.php as following : 如果要更改根目录,请以“ storage / app / uploads / ”为例,您可以将filesystem.php设置如下:

'disks' => [

    'local' => [
        'driver' => 'local',
        'root'   => storage_path('app/uploads/'),
    ],
    ...

then this following method would store a file in storage/app/uploads/file.txt: 那么以下方法会将文件存储在storage / app / uploads / file.txt中:

Storage::put('file.txt', 'Contents');

this would delete file.txt in storage/app/uploads directory. 这将删除storage / app / uploads目录中的file.txt。

Storage::delete('file.txt');

Something that took me quite some time to figure out today: 我今天花了很长时间才弄明白的事情:

In case you're trying to delete a list of files that are wrapped in Laravel's own Collection wrapper, you might need to call ->all() to get the underlying array instance: 如果您试图删除包含在Laravel自己的Collection包装器中的文件列表,您可能需要调用->all()来获取底层数组实例:

// The following is an example scenario
$files = Storage::list('downloads');

$collection = collect($files)->filter(function ($file) {
  return str_contains($file, 'temp');
});

Storage::delete($collection->all()); // Note the "all()" call here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM