You are here

function imce_copy_uploaded in IMCE 5

copy uploaded file to the specified directory.

1 call to imce_copy_uploaded()
imce_menu in ./imce.module
Implementation of hook_menu().

File

./imce.module, line 227

Code

function imce_copy_uploaded($set, $file) {

  //is the file real
  if (!is_uploaded_file($file['tmp_name']) || !isset($_SESSION['imcetotal'])) {
    drupal_set_message(t('No files were uploaded.'), 'error');
    return FALSE;
  }

  //move uploaded file to drupal's temp directory
  $temp = tempnam(file_directory_temp(), 'tmp_');
  register_shutdown_function('file_delete', realpath($temp));
  if (!move_uploaded_file($file['tmp_name'], $temp)) {
    drupal_set_message(t('File upload error. Could not move uploaded file.'));
    return FALSE;
  }
  $file['tmp_name'] = $temp;

  //dont like too long filenames and files without an extension.
  $file['name'] = trim(basename($file['name']), '.');
  $file['name'] = strlen($file['name']) > 128 ? substr($file['name'], -128) : $file['name'];

  //max filename lenght is 128
  if (!($dot = strrpos($file['name'], '.'))) {
    drupal_set_message(t('Invalid filename.'), 'error');
    return FALSE;
  }
  $extension = strtolower(substr($file['name'], $dot));

  //check image type and dimensions.
  if (in_array($extension, $GLOBALS['imce_ext']) && ($info = @getimagesize($file['tmp_name']))) {
    if (!in_array($info[2], array(
      1,
      2,
      3,
    ))) {

      // 1=gif, 2=jpg, 3=png. real file types
      drupal_set_message(t('Only images with .jpg, .gif, and .png extensions are allowed.'), 'error');
      return FALSE;
    }
    if ($info[0] > $set->width || $info[1] > $set->height) {
      if (($set->scale || $set->nolimit) && $_POST['scale']) {
        if (!image_scale($file['tmp_name'], $file['tmp_name'], $set->width, $set->height)) {
          drupal_set_message(t('Error scaling image.'), 'error');
          return FALSE;
        }
        clearstatcache();
        $file['size'] = filesize($file['tmp_name']);

        //new size
        $info = @getimagesize($file['tmp_name']);
        drupal_set_message(t('Image was scaled to allowed dimensions.'));
      }
      else {
        if (!$set->nolimit) {
          drupal_set_message(t('Image dimensions exceed the limit.'), 'error');
          return FALSE;
        }
      }
    }
  }
  else {
    if ($set->extensions) {
      if (!in_array($extension, explode(',', str_replace(' ', '', $set->extensions)))) {
        drupal_set_message(t('Invalid file extension. Allowed extensions are .jpg, .gif, .png, !ext', array(
          '!ext' => $set->extensions,
        )), 'error');
        return FALSE;
      }
    }
    else {
      if ($set->uid != 1) {

        //no limitation for user1 regarding non-image files.
        drupal_set_message(t('Only images with .jpg, .gif, and .png extensions are allowed.'), 'error');
        return FALSE;
      }
    }
  }

  //check quota, filesize.
  if ($file['size'] > $set->filesize * 1024 && !$set->nolimit) {
    drupal_set_message(t('File size exceeds upload limit.'), 'error');
    return FALSE;
  }
  $newtotal = $file['size'] + $_SESSION['imcetotal'];
  if ($newtotal > $set->quota * 1024 && !$set->nolimit) {
    drupal_set_message(t('You dont have enough free space.'), 'error');
    return FALSE;
  }

  //clear filename and copy file
  $cleared = preg_replace("/[^\\w\\-\\_]/", '_', substr($file['name'], 0, $dot)) . $extension;
  $newpath = file_create_filename($cleared, $set->dir);
  if (!@copy($file['tmp_name'], $newpath)) {
    drupal_set_message(t('Error copying the file.'), 'error');
    return FALSE;
  }

  //success
  @chmod($newpath, 0664);
  $_SESSION['imcetotal'] = $newtotal;
  $_SESSION['imcelatest'] = $newpath;
  drupal_set_message(t('Upload successful.'));

  //thumbnails
  if ($_POST['thumb'] && $info && $set->twidth && $set->theight && ($set->twidth < $info[0] || $set->theight < $info[1])) {
    $aspect = $info[1] / $info[0];
    if ($aspect < $set->theight / $set->twidth) {
      $w = (int) min($set->twidth, $info[0]);
      $h = (int) round($w * $aspect);
    }
    else {
      $h = (int) min($set->theight, $info[1]);
      $w = (int) round($h / $aspect);
    }
    if (imce_resize_image($set, basename($newpath), $w, $h, TRUE)) {
      drupal_set_message(t('Thumbnail created.'));
    }
    else {
      drupal_set_message(t('Thumbnail can not be created.'), 'error');
    }
  }
  return TRUE;
}