Error deleting Laravel 5 file

There is a problem associated with my laravel 5 web application regarding file deletion.

I want to delete a record from my table with the 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 in db and saved the image path in the brand_logo field, for example: ( / uploads / master / logocatagory_Computers and IT.png ).

When deleting an entry from the database, I also want to delete the image file from the uploads folder. But during the delete action, the file was not deleted. 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 exists in my directory, and the directory has a resolution of 777. !!

I also tried the Storage class to delete the file. But there is no way !!! ..

How can I solve this problem? Any ideas ... :(

+5
source share
4 answers

You are doing it wrong. You cannot delete a file from a URL, you must provide a PATH file not a URL

Edited by

Assuming the file you want to delete is in public/uploads/masters/logocatagory_Computers , you can do this:

 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); } 
+6
source

If you have the file Storage / app /, Storage :: does not work, change /Config/Filesystems.php, try putting your "local" in base_path () and comment on default storage_path (),

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

Then in Controller

 use Illuminate\Support\Facades\Storage; 

Then in public funtion use Storage Model

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

In Laravel 5.1. When using the local driver, note that all file operations are related to the root directory defined in your configuration file. By default, this value is set to the repository / application directory. Hence,

the following method will save the file in storage / app / file.txt:

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

this will delete the file.txt file in the repository / application

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

If you want to change the root directory, take ' storage / app / uploads / ', you can install the system.php file as follows:

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

then this next method will save the file in storage / app / uploads / file.txt:

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

this will delete the file.txt file in the storage / application / download directory.

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

Something that took me quite a while to understand today:

If you are trying to delete a list of files that are wrapped in your own Laravel collection shell, you may need to call ->all() to get an instance of the underlying array:

 // 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 
0
source

All Articles