diff --git a/library/think/File.php b/library/think/File.php index 11453810..27a1c258 100644 --- a/library/think/File.php +++ b/library/think/File.php @@ -26,18 +26,37 @@ class File extends SplFileObject protected $rule = 'date'; // 单元测试 protected $isTest; - // 上传文件信息 protected $info; - public function __construct($filename, $info = [], $isTest = false) + public function __construct($filename, $mode = 'r', $useIncludePath = false, $context = null) { - parent::__construct($filename); - $this->info = $info; - $this->isTest = $isTest; + parent::__construct($filename, $mode, $useIncludePath, $context); $this->filename = $this->getRealPath(); } + /** + * 是否测试 + * @param bool $test 是否测试 + * @return $this + */ + public function isTest($test = false) + { + $this->isTest = $test; + return $this; + } + + /** + * 设置上传信息 + * @param bool $info 上传文件信息 + * @return $this + */ + public function setUploadInfo($info) + { + $this->info = $info; + return $this; + } + /** * 获取上传文件的信息 * @param string $name @@ -94,7 +113,7 @@ class File extends SplFileObject */ public function isValid() { - if($this->isTest){ + if ($this->isTest) { return is_file($this->filename); } return is_uploaded_file($this->filename); @@ -131,8 +150,8 @@ class File extends SplFileObject } /* 移动文件 */ - if($this->isTest){ - rename($this->filename, $path.$savename); + if ($this->isTest) { + rename($this->filename, $path . $savename); } elseif (!move_uploaded_file($this->filename, $path . $savename)) { $this->error = '文件上传保存错误!'; return false; diff --git a/library/think/Request.php b/library/think/Request.php index f37284e2..7b7e640b 100644 --- a/library/think/Request.php +++ b/library/think/Request.php @@ -810,7 +810,7 @@ class Request if (empty($val['tmp_name'])) { continue; } - $item[$key] = new File($val['tmp_name'], $val); + $item[$key] = new File($val['tmp_name'])->setUploadInfo($val); } } return $item; @@ -818,7 +818,7 @@ class Request if($array[$name] instanceof File){ return $array[$name]; } elseif (!empty($array[$name]['tmp_name'])){ - return new File($array[$name]['tmp_name'], $array[$name]); + return new File($array[$name]['tmp_name'])->setUploadInfo($array[$name]); } } }