Ejemplo n.º 1
0
 private static function upload_file($file, $path, $prefix, $zip, $limits, $mimes)
 {
     $names = '';
     $errors = array();
     if (is_uploaded_file($_FILES[$file]['tmp_name'])) {
         $type = self::filter_mime($mimes, $_FILES[$file]['type']);
         if (!empty($mimes) && empty($type)) {
             $errors[$file][] = '_bad_mimetype';
         }
         $suffix = '';
         $action = '';
         // checks for canonical files
         if ($path == APATH . self::$file_path) {
             if ($type == 'img') {
                 // too big
                 if ($_FILES[$file]['size'] > $limits[3] * 1024) {
                     $errors[$file][] = '_file_size_is_too_big';
                 }
                 // pixel dimensions
                 $imageinfo = getImageSize($_FILES[$file]['tmp_name']);
                 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'] > $limits[4] * 1024) {
                     $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']));
         // exists? added suffix
         $name = X4Files_helper::get_final_name($path . $type, $tmpname . $suffix);
         if (empty($errors)) {
             // copy
             $check = X4Files_helper::copy_file($path . $type, $name, $_FILES[$file]['tmp_name']);
             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);
                 // set return or delete
                 if ($check) {
                     // handle zip
                     if ($zip) {
                         $check = X4Files_helper::zip_file($filename);
                         if (!$check) {
                             unlink($filename);
                             $errors[$file][] = '_zip_error';
                         }
                     }
                 } else {
                     // delete the file
                     unlink($filename);
                 }
             } else {
                 $errors[$file][] = '_upload_error';
             }
         }
     }
     if (!empty($errors)) {
         // if errors
         if (!empty($filename)) {
             // delete the file
             unlink($filename);
         }
         return array($errors, 0);
     } else {
         if (empty($errors)) {
             // if no errors
             if (!empty($filename)) {
                 // return the file name (only the name, no path)
                 return $name;
             } else {
                 $errors[$file][] = '_upload_error';
                 return array($errors, 0);
             }
         }
     }
 }