Form handler to add timestamp to filenames of uploaded files

  • I'm using a Perch Form with a series of up to 9 image upload fields. I'd like to add a timestamp to every filename of the uploaded images (currently Perch adds a timestamp only if a file with the same name already exists). The amended filename needs to be applied to the image file and saved in the form response.


    I'm assuming I need a custom app (I'm calling it ipltd_filename) to do this. This app should just rename the files and then redispatch to mbk_forms (to handle reCAPTCHA), which in turn redispatches to perch_forms.


    In my form template, the form will post to the new app and include the file upload fields:



    I'm stuck on how to build a form handler in a custom app. I think the app's runtime.php should be something like:



    Can anyone point me in the right direction? Or show any examples of handling files in a form handler?

  • You don't want the $API->on method, as it won't be called until after the form has been processed by the forms_app, which won't happen if you redispatch the form within the closure.


    In your ipltd_filename_form_handler() You'll just need to access the form attributes and rename them, so when you redispatch the form perch_forms handles it in the manner you want. You should be able to fetch the data as $SubmittedForm->data. You could also add an attribute to your file field such, as <perch:input type="file" attribute="timestamp"/> Which you can then access the field with $SubmittedForm->get_attribute_map('timestamp');. That will then return you the field name/ID which should make it easier to edit the data as you need

  • Byron - thanks so much for your help - apologies for being slow responding.


    Not sure I understand your suggestion about adding attribute="timestamp" to the form input.


    This is what I've done:



    It seems too simplistic!! I didn't expect to be able to save the new filename as easily as $details['name'] = $filename;


    Please shout if you can suggest any improvements. I've a bit more testing to do but it seems to be working.