Example #1
0
 /**
  * 文件上传处理
  *
  * @param string $name
  *
  * @return bool
  */
 protected static function deal($name = 'file')
 {
     // 判断文件是否上传
     if (!isset($_FILES[$name])) {
         self::$error = '上传文件不能为空!';
         return false;
     }
     // 获取上传文件相关信息
     self::$files = $_FILES[$name];
     // 判断文件上传是否成功
     if ($msg = self::getFailed()) {
         self::$error = $msg;
         return false;
     }
     // 判断上传文件类型是否合法
     if (!self::isAllowType()) {
         self::$error = '上传文件类型不允许!';
         return false;
     }
     // 判断上传文件大小是否合法
     if (!self::isAllowSize()) {
         self::$error = '上传文件大小不允许!';
         return false;
     }
     return true;
 }
Example #2
0
 protected final function setFiles($key, $req)
 {
     $file = new File($req["name"]);
     $file->tmp(isset($req["tmp_name"]) ? $req["tmp_name"] : "");
     $file->size(isset($req["size"]) ? $req["size"] : "");
     $file->error($req["error"]);
     $this->files[$key] = $file;
 }
Example #3
0
 /**
  * Function LoadFile
  * @param $file
  * @param $path
  * @param $name
  * @return mixed
  */
 public static function LoadFile($file, $path, $name, &$error = array(), &$data = array())
 {
     $format = mb_strtolower($file['name']);
     $format = mb_substr($format, mb_strrpos($format, '.') + 1);
     if (is_uploaded_file($file['tmp_name'])) {
         if ($file['size'] <= self::$allowedFileSize) {
             if (self::$allowedFileFormats[$format] === true) {
                 if (!file_exists($path) || !is_dir($path)) {
                     @mkdir($path, 0777);
                     @chmod($path, 0777);
                 }
                 if (copy($file['tmp_name'], $path . '/' . $name . '.' . $format)) {
                     $data['fileName'] = $file['name'];
                     $data['name'] = $name;
                     $data['format'] = $format;
                     $data['size'] = $file['size'];
                     $data['path'] = $path;
                     $data['url'] = $path . '/' . $name . '.' . $format;
                     return true;
                 } else {
                     self::$error = "Error while moving file to site storage.";
                     return false;
                 }
             } else {
                 self::$error = "WRONG_FORMAT";
                 return false;
             }
         } else {
             self::$error = "WRONG_SIZE";
             return false;
         }
     } else {
         self::$error = $file['error'];
         return false;
     }
 }
Example #4
0
 protected final function __set_files__($key, $req)
 {
     $file = new File($req['name']);
     $file->tmp(isset($req['tmp_name']) ? $req['tmp_name'] : '');
     $file->size(isset($req['size']) ? $req['size'] : '');
     $file->error($req['error']);
     $this->files[$key] = $file;
 }
Example #5
0
 /**
  *
  * do some config validation
  */
 public static function validateConf($config, $param)
 {
     // do some extra work on repository config query
     if ($param == 'shared_folder') {
         // error if repository does not exist
         if (!is_dir($config['shared_folder'])) {
             File::error('Directory does not exist: ' . $config['shared_folder']);
         }
     }
     // max_filesize check server's conflict
     if ($param == 'max_filesize') {
         $php_post_max_size = File::returnBytes(ini_get('post_max_size'));
         $php_upload_max_filesize = File::returnBytes(ini_get('upload_max_filesize'));
         if ($config['max_filesize'] > $php_post_max_size || $config['max_filesize'] > $php_upload_max_filesize) {
             //File::error('Config param max_filesize is bigger than php server setting: post_max_size = '.$php_post_max_size.', upload_max_filesize = '.$php_upload_max_filesize);
         }
     }
     // convert array to regexp
     if ($config['accept_file_extensions'] == '*' || in_array('*', $config['accept_file_extensions'])) {
         $config['accept_file_extensions'] = '/\\.+/';
     } else {
         $config['accept_file_extensions'] = '/(\\.|\\/)(' . implode('|', $config['accept_file_extensions']) . ')$/i';
     }
     // advanced: encryption salt
     $config['encryption_salt'] = $_SERVER['SERVER_NAME'];
     // advanced: mode when creating new directory (ignored on windows)
     $config['new_dir_mode'] = 0755;
     // strip trailing slash & forward slashes for fs
     $config['shared_folder'] = rtrim($config['shared_folder'], "/\\");
     $config['shared_foldery'] = str_replace('\\', '/', $config['shared_folder']);
     $config['base_url'] = rtrim($config['base_url'], "/\\");
     return $config;
 }
Example #6
0
File: File.php Project: kizz66/meat
 /**
  * Загружает файл на сервер
  * @param string $field имя поля
  * @return string имя загруженного файла; false в случае сбоя
  */
 public static function upload($pathToUpload = false, $field = 'Image', $single = true, $slugify = false)
 {
     if (!isset($_FILES[$field])) {
         return false;
     }
     if (is_array($_FILES[$field]["name"])) {
         foreach ($_FILES[$field] as $k => $v) {
             for ($i = 0; $i < count($v); $i++) {
                 $files[$i][$k] = $v[$i];
             }
         }
     } else {
         $files[] = $_FILES[$field];
     }
     $pathToUpload = $pathToUpload ? DOC_ROOT . $pathToUpload : self::$uploadDir;
     self::$error = '';
     $fileNameArr = array();
     for ($i = 0, $fileCount = count($files); $i < $fileCount; $i++) {
         if (!is_uploaded_file($files[$i]['tmp_name'])) {
             $error_code = !isset($files[$i]['error']) ? 4 : $files[$i]['error'];
             switch ($error_code) {
                 case 1:
                     // UPLOAD_ERR_INI_SIZE
                     self::$error = lang('upload_file_exceeds_limit', __CLASS__);
                     break;
                 case 2:
                     // UPLOAD_ERR_FORM_SIZE
                     self::$error = lang('upload_file_exceeds_form_limit', __CLASS__);
                     break;
                 case 3:
                     // UPLOAD_ERR_PARTIAL
                     self::$error = lang('upload_file_partial', __CLASS__);
                     break;
                 case 4:
                     // Нет файла и ладно
                     break;
                 case 6:
                     // UPLOAD_ERR_NO_TMP_DIR
                     self::$error = lang('upload_no_temp_directory', __CLASS__);
                     break;
                 case 7:
                     // UPLOAD_ERR_CANT_WRITE
                     self::$error = lang('upload_unable_to_write_file', __CLASS__);
                     break;
                 case 8:
                     // UPLOAD_ERR_EXTENSION
                     self::$error = lang('upload_stopped_by_extension', __CLASS__);
                     break;
                 default:
                     self::$error = lang('upload_no_file_selected', __CLASS__);
                     break;
             }
             continue;
         }
         if (!preg_match('/\\.([^.]*?)$/i', $files[$i]['name'], $extension)) {
             self::$error = lang('upload_bad_filename', __CLASS__);
             continue;
         }
         $extension = $extension[1];
         if (!in_array($files[$i]['type'], self::$acceptMimeTypes)) {
             self::$error = lang('upload_invalid_filetype', __CLASS__);
             continue;
         }
         if ($slugify) {
             $fileName = substr($files[$i]['name'], 0, -strlen($extension) - 1);
             $fileNameArr[$i] = self::_setUniqueName($pathToUpload, slugify($fileName), $extension);
         } else {
             $fileNameArr[$i] = self::_generateUniqueName($pathToUpload, $extension);
         }
         if (!@copy($files[$i]['tmp_name'], $pathToUpload . $fileNameArr[$i])) {
             if (!@move_uploaded_file($files[$i]['tmp_name'], $pathToUpload . $fileNameArr[$i])) {
                 self::$error = lang('upload_destination_error', __CLASS__);
                 continue;
             }
         }
         @chmod($pathToUpload . $fileNameArr[$i], 0666);
         self::setFileSize($files[$i]['size']);
     }
     if ($single && isset($fileNameArr[0])) {
         return $fileNameArr[0];
     } else {
         if (count($fileNameArr)) {
             return $fileNameArr;
         }
     }
     return false;
 }