0

In D7,I have new_form with a managed_file field to allow my user to upload documents to public://. I'm trying to prevent my user from uploading a document if the file name they are trying to upload already exists in the system. So far I've been successful using this:

function mymodule_file_validate($file) {
  $filename = $file->filename;
  $errors = array();
  if (file_exists('public://' . $filename)) { 
    $errors[] = t("File exists already!.");
  }
  return $errors;
}

This displays an error if the user browses for the file and presses upload, even before the file submission, which is exactly what I need.

However, I also have another form modify_form in this module that allows users to modify the document and in that case it's okay to upload the document with the same name even though the file name exists already. BUT... the function above blocks my modify_form too.

Any suggestions how to make the function apply only to my new_form? Or another way to prevent upload if file exists already only on new_form? Thanks!

2

I ended up adding a custom validator only to the new_form file upload field:

'#type' => 'managed_file',
'#upload_validators' => array(
      'file_validate_unique' => array($file),
)

and then added a function:

function file_validate_unique($file) { 
   $errors = array();
   $filename = $file->filename;
   if (file_exists('public://' . $filename)) { 
         $errors[] = t('The file could not be uploaded because a file by that name already exists in the destination directory.');
   }
   return $errors;
}

now it only validates the new_form and outputs an error right when the user tries to upload the managed_file, before the form is even submitted.

0

In my hook_form_alter(), I only wanted this limit on new uploads, so I have this:

// Set the upload field to disallow duplicate filenames.
$lang = $form['field_upload']['#language'];
$last = $form['field_upload'][$lang]['#file_upload_delta'];
$form['field_upload'][$lang][$last]['#upload_validators']['tta_custom_materials_upload_validation']
  = array(TRUE);

And my function looks like this:

function tta_custom_materials_upload_validation($file) {
  // Fetch filename from $form.
  $filename = $file->filename;

  $errors = array();

  if (file_exists(file_build_uri('materials/' . $filename))) {
    $errors[] = t("The new material file already exists. Please use a different name.");
  }

  return $errors;
}

This works exactly as you would want.

-1
  • Instead of putting it into hook_file_validate try attaching a form validation handler through hook_form_alter or hook_FORMID_form_alter. Add your validation handler with $form['#validate'][] = 'my_module_my_form_validation_function'
  • And then just write the code in the condition:

    function my_module_my_form_validation_function(&$form) {
     if(form_id == 'your_form_id') {
     $filename = $file->filename;    // fetch filename through $form
     $errors = array();
     if (file_exists('public://' . $filename)) { 
       $errors[] = t("File exists already!.");
      }
      return $errors;
     }
    }
    
  • Thanks Astha, I tried your solution but it looks like Drupal's file_managed module already renames the uploaded file before the form is submitted. Therefore the form validation route is already too late it seems? – longboardnode Jun 18 '14 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.