You are here

function _webform_validate_file in Webform 6.3

Same name and namespace in other branches
  1. 5.2 components/file.inc \_webform_validate_file()
  2. 5 components/file.inc \_webform_validate_file()
  3. 6.2 components/file.inc \_webform_validate_file()

A Form API element validate function.

Ensure that the uploaded file matches the specified file types.

1 string reference to '_webform_validate_file'
_webform_render_file in components/file.inc
Implements _webform_render_component().

File

components/file.inc, line 397
Webform module file component.

Code

function _webform_validate_file($element, &$form_state) {
  $component = $element['#webform_component'];
  $form_key = implode('_', $element['#parents']);
  if (empty($_FILES['files']['name'][$form_key])) {
    return;
  }

  // Build a human readable list of extensions:
  $extensions = $component['extra']['filtering']['types'];
  $extension_list = '';
  if (count($extensions) > 1) {
    for ($n = 0; $n < count($extensions) - 1; $n++) {
      $extension_list .= $extensions[$n] . ', ';
    }
    $extension_list .= 'or ' . $extensions[count($extensions) - 1];
  }
  elseif (!empty($extensions)) {
    $extension_list = $extensions[0];
  }
  if (in_array('jpg', $extensions)) {
    $extensions[] = 'jpeg';
  }
  $strrpos = function_exists('mb_strrpos') ? 'mb_strrpos' : 'strrpos';
  $dot = $strrpos($_FILES['files']['name'][$form_key], '.');
  $extension = drupal_strtolower(drupal_substr($_FILES['files']['name'][$form_key], $dot + 1));
  $file_error = FALSE;
  if (!empty($extensions) && !in_array($extension, $extensions)) {
    form_error($element, t("Files with the '%ext' extension are not allowed, please upload a file with a %exts extension.", array(
      '%ext' => $extension,
      '%exts' => $extension_list,
    )));
    $file_error = TRUE;
  }

  // Now let's check the file size (limit is set in KB).
  if ($_FILES['files']['size'][$form_key] > $component['extra']['filtering']['size'] * 1024) {
    form_error($element, t("The file '%filename' is too large (%filesize KB). Please upload a file %maxsize KB or smaller.", array(
      '%filename' => $_FILES['files']['name'][$form_key],
      '%filesize' => (int) ($_FILES['files']['size'][$form_key] / 1024),
      '%maxsize' => $component['extra']['filtering']['size'],
    )));
    $file_error = TRUE;
  }

  // Save the file to a temporary location.
  if (!$file_error) {
    $upload_dir = file_directory_path() . '/webform/' . $component['extra']['savelocation'];
    if (file_check_directory($upload_dir, FILE_CREATE_DIRECTORY)) {
      $file = file_save_upload($form_key, array(), $upload_dir);
      if ($file) {
        @chmod($file->filepath, 0664);

        // Set the hidden field value.
        $parents = $element['#array_parents'];
        array_pop($parents);
        $parents[] = '_fid';
        form_set_value(array(
          '#parents' => $parents,
        ), $file->fid, $form_state);
      }
      else {
        drupal_set_message(t('The uploaded file %filename was unable to be saved. The destination directory may not be writable.', array(
          '%filename' => $file->filename,
        )), 'error');
      }
    }
    else {
      drupal_set_message(t('The uploaded file was unable to be saved. The destination directory does not exist.'), 'error');
    }
  }
}