Upload file and remove the old

I have a page to insert record, that works perfectly, with a file field for uploading an PDF (not required)
Then I have a page to update records
My problem, which I could not solve with Wappler, occurs when a new PDF file is chosen for upload
I need to delete the old file from the server, if it exists
I can’t understand if I do to delete it before or after the new upload and how is it better to set the sequence of actions?
Can anyone help me?
Thank you

If the file has the same name, then just overwrite it:

image
(in File Upload Properties)

If it has a different name, I wouldn’t have thought it would make much difference at what point you delete the old file.

Of course it does not have the same name and theoretically should be eliminated only after successful uploading

In addition to Tom’s reply, if the file names are different:

Before the update, after the upload step, add a condition and check if the file upload uploaded a file. You want to delete the old file if a new one is uploaded:

– Upload step

– Condition (check upload step if it uploaded a file)

THEN

  • run a database query, filter by the record ID you are updating.
  • repeat step (repeats query)
    • if file exists (file name/path from the query)
      • file delete
    • if file doesn’t exist
      • do nothing

ELSE

  • do nothing

Update step and everything else goes here

2 Likes

so is the name stored in a database or is the pdf stored in a unique directory so the file name can be identified easily, you dont say

1 Like

Thank you
I had thought of such a procedure, but now it is clearer to me

1 Like

If it has the same purpose - eg supposing you had a link to a terms page which could be downloaded as a PDF - then it may be best if it does have the same name. In this case, various things are simpler: you just overwrite the old file and don’t need to update links etc… But of course it depends on the circumstances.

It’s true but if it’s the client who uploads the pdf …

That shouldn’t make any difference - just rename the file as it’s uploaded.

Eg I often upload text files to import into a MySQL database. It’s simpler for me if the upload filename is fixed. It doesn’t matter what the original file is called.

1 Like

Yes that’s a great idea indeed :slight_smile:

It’s a nice idea, but each record has a different file
I can rename the file with ID prefix
example -> 3456_filename.pdf

Yes you can use any dynamic expression in the file name template :slight_smile:

One last consideration
Should the file be renamed before uploading or after?

You cannot rename a file BEFORE uploading :slight_smile:
You rename it in the upload step.

Ok but does the rename action overwrite the existing file or is it necessary to delete the old file?

It renames the file you are uploading and saves it with the new name.

Perfect thanks
Another step forward with Wappler

Hi Tom
I’m trying to rename the file when uploading, but obviously my configuration is not correct
These are the two server actions
Do you know where I’m wrong?

37
54

{{$_POST.id}+’_bollo’}.{pdf}

Why not use the template field in the upload step, so you rename the file directly on upload? You don’t need a separate rename step with a file upload.

Also, your expression is wrong.
It should be: {{$_POST.id}}_bollo.{ext}

I didn’t realize that it could be renamed directly.
Perfect but it is necessary to remove the point before the extension, otherwise it is double
But now I have two problems:

  • The file name is not registered in the database
  • Unfortunately the insertion of the record number ID does not work because in fact being an insert page is not yet assigned