Buy Now
Sign in

FAQ search

How can I edit uploaded files in a form?

Details
Published: Wednesday, 08 April 2015 12:43

It isn't useful to redisplay the value of file inputs in an edit form as there is no way of knowing the path to the file on the user's computer and so the value is incomplete. Because of this most browsers won't allow it.

Instead you need to display a thumbnail image or a file name to show the user what the currently saved file is.

When the form is submitted, if the file upload inputs are left empty then, by default, these will over-write any saved values. To prevent this you need to add code to remove the corresponding entries from the $form->data array so that the existing file names are kept.

There is no entry for a file upload input in the $form->data array until after the Upload Files action runs so you need to add a Custom Code action after the Upload Files action and before the DB Save action and add this code to it replacing the array values with the names of the file inputs in your form.

<?php
$files_array = array(
  'file_upload_1',
  'file_upload_2',
  'file_upload_3',
  'file_upload_4'
};
foreach ( $files_array as $v ) {
  if ( !$form->data[$v] ) {
    unset($form->data[$v]);
  }
}
?>

This will remove any empty file-upload inputs completely from the $form->data array and therefore leave any existing values in the database unchanged.