Delete files using wildcard

I am using the file upload SC action and storing the file as record_id.ext (eg. 2.jpg) so the files are tidy. The action retains the uploaded extension (.jpg or .png for example) and renames the first part to the record id.

If someone uploads image.jpg (which saves as 2.jpg) and then re-uploads another.png (which saves as 2.png) I end up with two files - 2.jpg and 2.png. The database stores the filename so there’s no problem with the functionality but I have an orphaned file of 2.jpg.

So, I’m thinking that deleting id.* (eg. 2.) before doing the upload would keep everything tidy. But it’s not working! I get a 500 error saying 2. isn’t found.

Can anyone help with this?

I have struggled with the same problem for ages and finally managed solved it.
How does you file update/upload action look like?

This is the upload action:

Try this:

  • Dbase single query: qrydeleteimg
    • Columns: id and image
    • Condition: id equal {{$_POST.id}}
    • If file exists: folder/qrydeleteimg.afbeelding
      * Then
      * Steps
  1. Delete image
  2. File upload
  3. Load image
  4. Resize image
  5. Save image +overwrite
  6. Update dbase

Thanks for that. That’s kind of what I’ve had to implement but it would be so much slicker to be able to just say delete id.* and all the files are deleted in one go immediately before the new file is uploaded.

Ah, in that case you should try @ben 's gallery tutorials.
https://www.youtube.com/watch?v=sZedCBgy-0I&list=PLUjqTJN3byC9kK8YZkmGwjo9nrEipl2yb&index=5

Thanks again. This requires structuring it so the files go in a sub-folder named after the id. I’m not against doing that but it would mean a number of changes across the site so I’d still rather have a way to delete files using a wildcard.

You can do that by

  • adding a Repeat for the query and in steps add the if file exists in then the delete process.
  • in else add the upload

The problem is that there isn’t a query that would show all the files. If someone uploads a .jpg and then replaces it with a .png, both files will be in the folder but the record will only store the .png. So there’s an orphan .jpg file. That’s why a simple wildcard delete would be the best solution.

So give that someone a "“Delete before update”, so you never have an orphan image.

Yeah, that’s probably the best solution. I’d still like a wildcard delete, though. :slight_smile: