123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- class UploadedFile
- {
- static private $_files;
- private $_name;
- private $_tempName;
- private $_type;
- private $_size;
- private $_error;
- static $_this;
- /**
- * 获取实例
- *
- * @param 你上传的key
- * @return 返回该类的一些属性,见下面的构造函数
- * 如果没有文件上传,返回的是null
- */
- public static function getInstanceByName($name)
- {
- if(null===self::$_files)
- self::prefetchFiles();
- return isset(self::$_files[$name]) && self::$_files[$name]->getError()!=UPLOAD_ERR_NO_FILE ? self::$_files[$name] : null;
- }
- /**
- * 遍历出 $_FILES 的元素属性
- *
- */
- protected static function prefetchFiles()
- {
- self::$_files = array();
- if(!isset($_FILES) || !is_array($_FILES))
- return;
- foreach($_FILES as $class=>$info)
- self::collectFilesRecursive($class, $info['name'], $info['tmp_name'], $info['type'], $info['size'], $info['error']);
- }
- /**
- * @param string $key FILE的key
- * @param mixed $names 文件名
- * @param mixed $tmp_names 临时文件名
- * @param mixed $types 文件类型
- * @param mixed $sizes 文件大小
- * @param mixed $errors 错误码
- */
- protected static function collectFilesRecursive($key, $names, $tmp_names, $types, $sizes, $errors)
- {
- if(is_array($names))
- {
- foreach($names as $item=>$name)
- self::collectFilesRecursive($key.'['.$item.']', $names[$item], $tmp_names[$item], $types[$item], $sizes[$item], $errors[$item]);
- }
- else
- self::$_files[$key] = new UploadedFile($names, $tmp_names, $types, $sizes, $errors);
- }
- /**
- * 构造函数.
- * @param string $name 上传时候的文件名
- * @param string $tempName 临时文件
- * @param string $type 文件类型 (如 "image/gif").
- * @param integer $size 文件大小
- * @param integer $error 错误码
- */
- public function __construct($name,$tempName,$type,$size,$error)
- {
- $this->_name=$name;
- $this->_tempName=$tempName;
- $this->_type=$type;
- $this->_size=$size;
- $this->_error=$error;
- self::$_this = $this;
- }
- public function __toString()
- {
- return $this->_name;
- }
- /**
- * 保存文件
- * @param string $file 保存文件的地方
- * @param boolean $deleteTempFile 上传成功后,是否删除
- * @return boolean
- */
- public static function saveAs($file,$deleteTempFile=true)
- {
- if(self::$_this->_error==UPLOAD_ERR_OK) {
- if($deleteTempFile) {
- return move_uploaded_file(self::$_this->_tempName,$file);
- }
- elseif(is_uploaded_file(self::$_this->_tempName)) {
- return copy(self::$_this->_tempName, $file);
- }
- else {
- return false;
- }
- }
- else {
- return false;
- }
- }
- public function getName()
- {
- return $this->_name;
- }
- public function getTempName()
- {
- return $this->_tempName;
- }
- public function getType()
- {
- return $this->_type;
- }
- public function getSize()
- {
- return $this->_size;
- }
- public function getError()
- {
- return $this->_error;
- }
- public function getHasError()
- {
- return $this->_error!=UPLOAD_ERR_OK;
- }
- public function getExtensionName()
- {
- if(($pos=strrpos($this->_name,'.'))!==false)
- return (string)substr($this->_name,$pos+1);
- else
- return '';
- }
- }
|