Exemplo n.º 1
0
 /**
  * Files upload
  * Perform the upload of an array of files with all required checks: file size, image size form images (crop and resize), check for overwrite and add prefixes, store the file inthe right folder.
  *
  * @param string	the name of the field
  * @param string	the path where to store the file. The standard path is APATH.'files/filemanager/'
  * @param string	a prefix for the filename
  * @param boolean	zip the file  if it not an image and is set to true
  * @param array		the array contains (maximum width, maximum height, action_string). Possibles values for the action string are: 'NONE', 'CROP', 'RESIZE', 'SCALE'
  * @param array		the array contains valid mime types
  * @return mixed	the filename string if upload only a file, an array of filename if upload many files
  */
 private static function upload_files($file, $path, $prefix, $zip, $limits, $mimes)
 {
     $names = array();
     $error = array();
     $n = sizeof($_FILES[$file]['tmp_name']);
     for ($i = 0; $i < $n; $i++) {
         if (is_uploaded_file($_FILES[$file]['tmp_name'][$i])) {
             // define the final folder
             $type = self::filter_mime($mimes, $_FILES[$file]['type'][$i]);
             $suffix = '';
             $action = '';
             if (!empty($mimes) && empty($type)) {
                 $errors[$file][] = '_bad_mimetype';
             }
             if ($type == 'img' && $path == APATH . self::$file_path) {
                 // too big
                 if ($_FILES[$file]['size'][$i] > $limits[3] * 1024) {
                     $errors[$file][] = '_file_size_is_too_big';
                 }
                 // pixel dimensions
                 $imageinfo = getImageSize($_FILES[$file]['tmp_name'][$i]);
                 if (!X4Files_helper::checkImageSize($imageinfo, $limits)) {
                     if ($limits[2] == 'NONE') {
                         $errors[$file][] = '_image_size_is_too_big';
                     } else {
                         $action = $limits[2];
                     }
                 }
             } else {
                 // too big
                 if ($_FILES[$file]['size'][$i] > $limits[4] * 1024 && $path == APATH . self::$file_path) {
                     $errors[$file][] = '_file_size_is_too_big';
                 }
             }
             // handle type and folders
             $type = $path == APATH . self::$file_path ? $type . '/' : '';
             // file name
             $tmpname = X4Utils_helper::unspace(strtolower($prefix . $_FILES[$file]['name'][$i]));
             // exists?
             $name = X4Files_helper::get_final_name($path . $type, $tmpname);
             if (empty($errors)) {
                 // copy
                 $check = X4Files_helper::copy_file($path . $type . '/', $name, $_FILES[$file]['tmp_name'][$i]);
                 if ($check) {
                     // define the filename with the complete path
                     $filename = $path . $type . $name;
                     // switch between actions
                     // NOTE: source and destination are the same
                     $check = self::set_action($action, $filename, $filename, $limits);
                     // return or delete
                     if ($check) {
                         $names[$name] = $filename;
                     } else {
                         unlink($filename);
                     }
                 } else {
                     $errors[$file][] = '_upload_error';
                 }
             }
         } else {
             if (empty($names)) {
                 $errors[$file][] = '_upload_error';
             }
         }
     }
     if (empty($errors)) {
         return array(array_keys($names), 1);
     } else {
         foreach ($names as $k => $v) {
             // delete the file
             unlink($v);
         }
         return array($errors, 0);
     }
 }