Editor PHP Custom Upload question
Editor PHP Custom Upload question
I have a custom upload function which takes an uploaded file and sends it to a cloud service.
At the moment the file uploads to a folder for which the name is hardcoded into the php script.
I'd like to set this folder name based upon another field set in the editor form but I can't work out how to get this value into the custom upload function.
I can set the name to be a variable and this works but it's how I get the variable to be set from another Editor field that's proving tricky for me. I'm wondering if a preUpload event might work somehow but I'm struggling.
Any ideas would be appreciated.
This question has an accepted answers - jump to answer
Answers
Use preCreate or preEdit or something else from this list: https://editor.datatables.net/manual/php/events
and set a session variable with whatever you need from $values - (The values submitted by the client-side).
Subsequently use the content of the session variable for your folder file etc name - and maybe unset it subsequently.
Hi rf1234
Thanks for you answer but I'm still struggling. preEdit and preCreate only fire on hitting the submit button. The file is uploaded before this happens.
preUpload is on that list but it doesn't use $values.
So I'm still kind of stuck.
Hmmm...
"preEdit and preCreate only fire on hitting the submit button. The file is uploaded before this happens."
Sounds like you are editing an existing record by uploading something. Then it is an easy one: Use a getFormatter to set the session variable!
here is an example:
Roland
Thanks anyway Roland. It's not an essential for my project but it would be great if I can get this working.
My recommendation doesn't make sense for you, I see. It only works under quite special circumstances and I could replace it myself by using $data[table.id] in the getFormatter of a different field in that same table.
The only other thing I can think of is you set the session variable through ajax based on a suitable java script event. Here is an example:
There are two stages to this. The first is to send that extra information to the server - for that you need to use the
ajaxData
option of theupload
field type. That will let you assign extra data to be submitted. You will need to make sure that the user enters that data before doing the upload though!Secondly - use a custom upload action. That will let you have complete control over where the file ends up and you can create your own path and move the file there using whatever data is required, including the information submitted.
Regards,
Allan
Thanks Roland / Allan
So if I add the following ajaxData line to my javascript for the upload field per the below this will feed the data from the "site" select field into the upload function in my PHP? Appreciating Allan your point that the user will have to have made the selection first.
If this is correct how do I "read" the data in the PHP function. What would I write in here to get the $sitenamevariablefromajaxdata string?
My javascript above is bunkem.
Looking at the Network XHR Params in Firefox the data is sent in the request payload but the tablerow part simply returns that string of text. Removing the quotation marks doesn't work either. Apologies, I lifted that bit of code from another erroneous forum post. If I try the below the value it comes back undefined.
Therefore is this where Roland's
bit comes in?
"I'd like to set this folder name based upon another field set in the editor form ..."
Can't help you with Allan's recommendation but you could resolve the issue with this:
In actions.php (or whatever you call it) you fill the $_SESSION variable and subsequently use it in the PHP upload instance.
More on dependent: https://editor.datatables.net/reference/api/dependent()
Thanks Roland.
I'll work on that. I think that's probably the final piece as I've now got the variable returning in the PHP from the XHR request. I can get the custom script to name a folder "this_is_a_test" on the cloud service using the $directory variable. All I need to do now (hopefully) is implement the dependent field part your have advised to replace "this_is_a_test" with the field value.
Roland / John
Many thanks for your help. Now all working. I went with Roland's answer and abandoned my code above.
In the PHP script this line got the value and assigned it to the variable that I could use in the Custom Upload script.