Change the upload name of a file in Editor 2.0.10
Change the upload name of a file in Editor 2.0.10
![Mairie du Pecq](https://secure.gravatar.com/avatar/e885cd70671c8e7b0e038742fe396937/?default=https%3A%2F%2Fvanillicon.com%2Fe885cd70671c8e7b0e038742fe396937_200.png&rating=g&size=120)
Hi,
I try to change the name of an uploaded file with the value of a field.
I was find this, but in Editor 2.0.10 that doesn't work
Field::inst( 'decisions.dec_titre' )
Field::inst( 'decisions.dec_file1_id' )
->upload(
Upload::inst( function ($file, $id, $data)) {
$titre = $data['decisions']['dec_titre'];
$newName = $titre . '.' . pathinfo($file['name'], PATHINFO_EXTENSION);
$filePath = '/mnt/' . $data['decisions']['dec_annee'] . '/' . $newName;
move_uploaded_file($file['tmp_name'], $filePath);
return $filePath;
})
...
I have doing the update (2.2.2) to try if that work, but same problem.
Is there a way to do that ?
This question has accepted answers - jump to:
Answers
No. Unfortunately that is not possible - the reason is that the file upload is async to the rest of the form. i.e. as soon as you select the file, it is uploaded. Even if that is because the user has entered anything else into the form. So your field might not have a value.
You also need to consider what would happen if the user gave the same name for different files. It could just report an error, but then the user is left guessing at a file name that hasn't been used before.
You could use the
ajax
option ofupload
to add data to send to the server (i.e. the field's value for use in your upload handler) and usepreUpload
to validate that there is a value before sending the file, but I feel it would be unintuitive.Allan
Allan is of course right with his point. But you could update the file name either in a separate batch routine or you use "writeEdit" and "writeCreate, or "validatedEdit" and "validatedCreate". You can read the file names using your own SQL and then update the file names if they are not identical to your field contents.
If you do it that way you would have the file names updated even before they are read back from the server.
https://editor.datatables.net/manual/php/events
On many occasions I do additional updates on "writeEdit" or "writeCreate" because I want to make sure that all updates have been made before the data is read back from the data base.
This is a simple example. I call a function that makes additional database updates using Editor's db handler and Editor's "raw" and "insert" methods. Of course you can use your own db-handler and use "global" instead of using Editor's db handler.
Thx @allan et @rf1234 for your feedback !
But, in this example :
That work, my file become 4.docx
In my case, that on an Edit bouton, with all the information already write before the upload action
Do i need to push the information in the php script like with "annee" ?
I only need the information in the field "decisions.dec_titre" to compose the file name that is already in DB.
The upload will happen before the rest of the form is submitted.
If the end user has already filled in the contents of the field
decisions.dec_titre
you need to use thedata
option of theajax
configuration for theupload
field type to add the data to send to the server, so that you can do$_POST['dec_titre
]` for example.Does that make sense?
Allan
Thx Allan for your time and explanations.
It works well !