1

I am using FilePond for the image upload. It generates random strings for the images uploaded and is saved in database as Array.

I tried to do the following, it deletes the record but doesn't delete the files associated with it.

Since it's an array, I tried this:

foreach($this->attachments as $attachment) {
            if(File::exists(storage_path($attachment))) {
                Storage::delete($attachment);
            }
        }

Also tried this:

if(Storage::exists($this->attachments))
{
  Storage::delete($this->attachments);
}

Note: I am using Filament as the admin dashboard.

The files are being saved at storage/app/public/vehicle-images

I did php artisan storage:link, so the public folder shows ``public/storage/vehicle-images```

Abdulrahman Mushref
  • 1,012
  • 2
  • 18
  • 40
  • 1
    Changing `File` to `Storage` **in the loop** should work. `File` is a facade that can manipulate the entire filesystem while `Storage` works with your configured storage drives in `config/filesystems.php` – apokryfos Jan 04 '23 at 06:40
  • @apokryfos Still not working. I tried adding this line: ```storage_path('vehicle-images/' . $attachment)``` The file is being saved in ```public/storage/vehicle-images```. – Abdulrahman Mushref Jan 04 '23 at 06:55
  • "I tried adding this line: storage_path('vehicle-images/' . $attachment) " <<<=== wait! Goback 1 step. Update your original question with this info, it's important. Of course doublecheck the file is there. When your file is in `public/storage` it should also be un your storage_path, but of course in the `public` directory – UnderDog Jan 04 '23 at 07:04
  • 1
    @UnderDog Updated the question. ```storage/app/public/vehicle-images``` – Abdulrahman Mushref Jan 04 '23 at 07:20

2 Answers2

3

In this example, the file exists in: Laravel/storage/app/public/vehicle-images

$filename = 'test.txt';

if(\File::exists(storage_path('app/public/vehicle-images/'.$filename))){
   \File::delete(storage_path('app/public/vehicle-images/'.$filename));
}

To better understand where the files are, and after that you can simply foreach loop check/delete.

You can read more on this here: https://laravel.com/docs/9.x/filesystem#the-public-disk

You can also later on specify a disk for that folder to make things easier to access.

Finally:

foreach($this->attachments as $attachment) {

    //Option #1: if $attachment == file.txt
    //use this code:
    if(\File::exists(storage_path('app/public/vehicle-images/'.$attachment))){
       \File::delete(storage_path('app/public/vehicle-images/'.$attachment));
    }
    
    //Option #2: if $attachment == vehicle-images/file.txt
    //use this code:
    if(\File::exists(storage_path('app/public/'.$attachment))){
       \File::delete(storage_path('app/public/'.$attachment));
    }

}

If you can show me how the filePond array looks like, I can adjust the code to work with it.

RG Servers
  • 1,726
  • 2
  • 11
  • 27
  • Tried it, still not deleting. – Abdulrahman Mushref Jan 04 '23 at 07:42
  • 3
    I tested this on a freshly installed laravel folder, and worked well. I've had issues in the past with linux folder permissions, can you try to manually create a **test.txt** file in a new folder, and do **dd(\File::exists(storage_path('app/public/testing/test.txt')));** if that succeeds try to delete it, if you fail then you have a folder permission issue – RG Servers Jan 04 '23 at 07:45
  • 2
    If you succeed in deleting the test.txt file and don't have a folder permission issue, then please share an example **DB stored array of attachments**, and I will adjust the code to work for you in the foreach loop or alternatives depends on how your data looks like. – RG Servers Jan 04 '23 at 07:53
  • I uploaded an image and both `````` and ```dd()```. Both are showing false! The file is there though – Abdulrahman Mushref Jan 04 '23 at 08:04
0

I got it. FilePond doesn't only store the image name, it also stores the url/folder the image was saved in. So instead of image.png, it is vehicle-images/image.png.

The code should be:

File::exists('storage/' . $attachment)

Which will read as: storage/ + vehicle-images/image.png.

Working code block:

foreach ($this->attachments as $attachment) {
            if (File::exists('storage/' . $attachment)) {
                File::delete('storage/' . $attachment);
            }
        }
Abdulrahman Mushref
  • 1,012
  • 2
  • 18
  • 40