You are here

function qqFileUploader::handleUpload in TinyBrowser 7

Returns 'Success' or 'Error: (error message)' string

File

tinybrowser/fileuploader.php, line 123

Class

qqFileUploader

Code

function handleUpload($uploadDirectory, $replaceOldFile = FALSE) {
  if (!is_writable($uploadDirectory)) {
    error_log("handleUpload: Server error. Upload directory [{$uploadDirectory}] os not writable.", 3, 'error.log');
    return 'Error: Upload directory ' . $uploadDirectory . ' is not writable';
  }
  if (!$this->file) {
    error_log("handleUpload: No files were uploaded.", 3, 'error.log');
    return 'Error: No files were uploaded';
  }
  $size = $this->file
    ->getSize();
  if ($size == 0) {
    error_log("handleUpload: File is empty.", 3, 'error.log');
    return 'Error: File is empty';
  }

  // if sizeLimit is 0, then we do not check max file size
  if ($this->sizeLimit > 0 && $size > $this->sizeLimit) {
    error_log("handleUpload: File is too large.", 3, 'error.log');
    return 'Error: File is too big';
  }

  // quota support with quick upload
  if ($tinybrowser['quota'][$typenow] > 0) {
    $ret = dirsize($tinybrowser['docroot'] . $tinybrowser['path'][$typenow]);
    $remain_space = $tinybrowser['quota'][$typenow] - $ret['size'];
    if ($remain_space < 0) {
      $remain_space = 0;
    }
    if ($remain_space < $size) {
      error_log("handleUpload: Quota error, allocated space is full", 3, 'error.log');
      return 'Error: Allocated disk space is full.';
    }
  }
  $pathinfo = pathinfo($this->file
    ->getName());
  $filename = $pathinfo['filename'];

  //$filename = md5(uniqid());
  $ext = $pathinfo['extension'];
  if ($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)) {
    $these = implode(', ', $this->allowedExtensions);
    error_log("handleUpload: File has an invalid extension. it should be one of {$these} .", 3, 'error.log');
    return 'Error: File has an invalid extension';
  }
  if (file_exists($uploadDirectory . $filename . '.' . $ext)) {
    if ($tinybrowser['upload_mode'] == 3) {

      // keep the existing one, reject the new one
      return 'Error: The same file name already exists';
    }
    else {
      if ($tinybrowser['upload_mode'] == 2) {

        // keep the existing one, rename the new one
        for ($idx = 1;; $idx++) {
          $dest_filename = $uploadDirectory . $filename . '-' . $idx . '.' . $ext;
          if (!file_exists($dest_filename)) {
            $filename .= '-' . $idx;
            break;
          }
        }
      }
    }
  }

  // replace the exiting one with the new one
  if ($this->file
    ->save($uploadDirectory . $filename . '.' . $ext)) {
    return 'Success';
  }
  else {
    return 'Error: Could not save uploaded file.';
  }
}