Drupal 7: file upload custom validation

Let’s say you want to validate an uploaded file so that you can check for a correct file naming convention. Here’s what you can do: use hook_file_validate, and don’t make the mistake of trying to use hook_node_validate as I did.

Once hook_node_validate fires, the file has already been saved. hook_file_validate allows us to prevent the file from being saved in the first place.

* Implements hook_file_validate()
function mymodule_file_validate($file) {
  $filename = $file->filename;
  $errors = array();
  if ($filename=='forbidden-file.pdf') {
    $errors[] = t("You are uploading a FORBIDDEN FILE.");
  return $errors;
Posted in drupal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: