You are here

function _imagefield_widget_upload_validate in ImageField 5.2

1 call to _imagefield_widget_upload_validate()
_imagefield_widget_prepare_form_values in ./imagefield.module

File

./imagefield.module, line 873
Defines an image field type. imagefield uses content.module to store the fid, and the drupal files table to store the actual file data.

Code

function _imagefield_widget_upload_validate($node, $field, $items, $file) {

  // initialize our validation state, innocent until proven guilty.
  $valid = true;

  // Do we even need to test file extensions?
  if (!empty($field['widget']['file_extensions'])) {

    // Pop out the extensions and turn file_extensions into an array.
    $ext = strtolower(array_pop(explode('.', $file['filename'])));
    $allowed_extensions = array_unique(explode(' ', strtolower(trim($field['widget']['file_extensions']))));

    // Is it the file extension in the allowed_extensions array?
    if (!in_array($ext, $allowed_extensions)) {

      // Sorry no..
      form_set_error($field['field_name'], t('Files with the extension %ext are not allowed. Please upload a file with an extension from the following list: %allowed_extensions', array(
        '%ext' => $ext,
        '%allowed_extensions' => $field['widget']['file_extensions'],
      )));
      $valid = false;
    }
  }

  // If max filesize is set.
  if (!empty($field['widget']['max_filesize'])) {
    if ($file['filesize'] > $field['widget']['max_filesize'] * 1024) {
      form_set_error($field['field_name'], t('The file you uploaded has a filesize greater than the maximum allowed filesize of %sizekb.', array(
        '%size' => $field['widget']['max_filesize'],
      )));
      $valid = false;
    }
  }

  // Is the mime type a match for image.
  if (strpos($file['filemime'], 'image/') !== 0) {

    // sorry no it isn't. do not pass go, do not collect $200.
    form_set_error($field['field_name'], t('Mime Type mismatch. Only image files may be upload. You uploaded a file with mime type: %mime', array(
      '%mime' => $file['filemime'],
    )));
    $valid = false;
  }

  // If max number of images is set
  if ($field['multiple'] && !empty($field['widget']['max_number_images'])) {
    $count = count($items) + count($_SESSION['imagefield'][$field['field_name']]);
    if ($count >= $field['widget']['max_number_images']) {
      form_set_error($field['field_name'], t('You are only allowed to upload up to %maximages images.', array(
        '%maximages' => $field['widget']['max_number_images'],
      )));
      $valid = false;
    }
  }
  return $valid;
}