From 70f146f3e31f0ec2b27e4c68eb027291e656a2a6 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 21 Jun 2016 11:22:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BFile=E7=B1=BB=E7=9A=84?= =?UTF-8?q?=E6=9E=B6=E6=9E=84=E6=96=B9=E6=B3=95=20=E6=94=AF=E6=8C=81SplFil?= =?UTF-8?q?eObject=E7=B1=BB=E5=AE=8C=E6=95=B4=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/File.php | 35 +++++++++++++++++++++++++++-------- library/think/Request.php | 4 ++-- 2 files changed, 29 insertions(+), 10 deletions(-) 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]); } } }