Upload file and remove the old

What have you picked as a value for insert?

Well, the inserted record ID is available after the insert. I see you are using some post variable $_POST.id not the record ID.

The problem file name in the database is automatically solved when the rename template is correct (I tried to rename it test {ext}
To put the number of record before the file name I think it is impossible, because it’s an incremental field
So I can’t find an acceptable solution to mark the file with a logical choice

It could be done with a query, getting the last record ID, then add +1 to it for the file name.

I also thought of this solution but I don’t know if problems can occur. If for example in the meantime a record is registered by another user
now I must to search another solution

Id’s returned are unique to the transaction and are therefore not influenced by other transactions, they remain unique

I thought about it and I decided not to rename the files during the record insertion.
It was a nice idea to catalog the files with a certain logic, but it is better not to complicate things
Also currently insert page works wonderfully
So I think I used the rename file in the update page to overwrite (if it already exists) the file already registered in the server

Just for understanding well:

  • Upload step: I understand this
  • Condition: like condition: {{upload1}}?
    THEN
  • Run a dbase query, filter by record ID: should I ad a Variable for record ID and then filter the query like “id == VariableID”?

Now I have three images to be updated/uploaded. Shoud I repeat this for every image?
Pff. I have been trying to figure this out for ages now, and still can’t get it to work.

What is the variable for? Just filter as you normally filter a query.

So at last I managed to figure it out. On update, the old image will be deleted from the folder and the now is being uploaded. But now it will not update the database record with the new file name. I am going nuts on this issue.

If it’s updating the database, then you haven’t configured this properly. Please double check your server action steps setup.

I had it all working on another project but now it keeps throwing errors at me.
These are my steps:

  1. Dbase connection
  2. Condition 2_condition
  3. Uploadstep
  4. Load image 4_loadimg
  5. Resize image 5_resize
  6. Save image
  7. Query image to be deleted
  8. If file exists 8_fileexist
  9. Delete file 9_fileremove

It throws this error
{"code":0,"file":"\/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/lib\/core\/Path.php","line":41,"message":"Invalid Path! ","trace":"#0 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/modules\/image.php(32): lib\\core\\Path::toSystemPath(NULL)\n#1 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/lib\/App.php(163): modules\\image->load(Object(stdClass), 'image1')\n#2 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/lib\/App.php(128): lib\\App->execSteps(Object(stdClass))\n#3 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/lib\/App.php(98): lib\\App->execSteps(Array)\n#4 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/modules\/core.php(82): lib\\App->exec(Object(stdClass), true)\n#5 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/lib\/App.php(163): modules\\core->condition(Object(stdClass), '')\n#6 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/lib\/App.php(128): lib\\App->execSteps(Object(stdClass))\n#7 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/lib\/App.php(98): lib\\App->execSteps(Array)\n#8 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnectLib\/lib\/App.php(71): lib\\App->exec(Object(stdClass))\n#9 \/usr\/home\/makkumercourant.nl\/htdocs\/dmxConnect\/api\/Content\/advertzij\/update.php(374): lib\\App->define('{\\n \"meta\": {\\n ...')\n#10 {main}"}<br /> <b>Fatal error</b>: Uncaught exception 'ErrorException' with message 'imagedestroy() expects parameter 1 to be resource, null given' in /usr/home/makkumercourant.nl/htdocs/dmxConnectLib/lib/image/Processor.php:25 Stack trace: #0 [internal function]: exception_error_handler(2, 'imagedestroy() ...', '/usr/home/makku...', 25, Array) #1 /usr/home/makkumercourant.nl/htdocs/dmxConnectLib/lib/image/Processor.php(25): imagedestroy(NULL) #2 [internal function]: lib\image\Processor-&gt;__destruct() #3 {main} thrown in <b>/usr/home/makkumercourant.nl/htdocs/dmxConnectLib/lib/image/Processor.php</b> on line <b>25</b><br />

Only thing I can see that could be an issue, and I am not certain of this as I have never tried it the way you have done it, is in your 3. Uploadstep you have not specified any template at all. I would add {name}{ext} in there.

Deleted the whole update including the form. It will now upload the new image, but does not delete the old one and it will ONLY update the dbase table when I select an image. If I want to keep the image and make changes through another field it will not update, but throw an error.
When I delete the “if fileexists” part and just keep the upload, load, resize and save image. It throws the same error saying #0 [internal function]: exception_error_handler(2, 'imagedestroy() ...', '/usr/home/makku...', 25, Array) #1 /usr/home/makkumercourant.nl/htdocs/dmxConnectLib/lib/image/Processor.php(25): imagedestroy(NULL) but there is no action to delete (destroy). Or am I misinterpreting again?

@psweb You say you do it in another way. Would you mind sharing how you would do a “delete image before update” please?

@web-works, just curious, what file type are you uploading?

I ask because when it is a non-image file and there is a resize server step, I get that error.

It’s a .jpg file

Your expression is wrong. You need the path to the file to be deleted, you passed the whole query as an expression. That’s wrong.
And no, Paul - file naming expression is not required, it is required if you want to change the uploaded file name. So better use a single query, which returns the values directly in the data picker.

@web-works you probably have some wrong expression somewhere in the server action again…

1 Like

By “path” you mean /uploadadvert/ or {{queryimage}}? The first on I have to input manually, the second I can select from the lightning bolt.
Now, If I only upload a new image it will upload, no more errors. But old image does not delete upon uploading new one.
However, when changing another field without selecting a new image I get the debug error again.
I am so confused again.
I added the link so you can test for yourself.
https://www.makkumercourant.nl/content/advertentieszij.php

Is a wrong expression. It returns the WHOLE QUERY, not a specific binding.
In order to access a specific binding from the query, you need a repeat step, or instead use the new option single query, it will return the query bindings in the data picker.

I really cannot understand what are you trying to do and what do you have in your server action actually.
I have a feeling that you lack some basic understanding of how the server connect and its steps work.
You should really take a look at the most basic server side docs.

aha, I missed out on the new option single query.
Thanks