From f9f53c032b54dd8671909566bb1b8b69099b345b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Fri, 6 May 2016 18:50:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0UploadFile=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/UploadFile.php | 91 ++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 library/think/UploadFile.php diff --git a/library/think/UploadFile.php b/library/think/UploadFile.php new file mode 100644 index 00000000..f809902a --- /dev/null +++ b/library/think/UploadFile.php @@ -0,0 +1,91 @@ + +// +---------------------------------------------------------------------- + +namespace think; + +class UploadFile +{ + + /** + * 上传文件信息 + * @var array + */ + protected $info = []; + + /** + * 上传错误信息 + * @var string + */ + private $error = ''; + + public function __construct($file) + { + $this->info = $file; + } + + protected function checkPath($path) + { + + if (is_dir($path)) { + return true; + } + + if (mkdir($path, 0777, true)) { + return true; + } else { + $this->error = "目录 {$savepath} 创建失败!"; + return false; + } + } + + /** + * 移动文件 + * @param string $path 保存路径 + * @param string $savename 保存的文件名 + * @param boolean $replace 同名文件是否覆盖 + * @return boolean 保存状态,true-成功,false-失败 + */ + public function moveTo($path, $savename = '', $replace = true) + { + if (false === $this->checkPath($path)) { + return false; + } + + $savename = $savename ?: $this->info['name']; + /* 不覆盖同名文件 */ + if (!$replace && is_file($path . $savename)) { + $this->error = '存在同名文件' . $path . $savename; + return false; + } + + /* 移动文件 */ + if (!move_uploaded_file($this->info['tmp_name'], $path . $savename)) { + $this->error = '文件上传保存错误!'; + return false; + } + $this->info['path'] = $path; + $this->info['savename'] = $savename; + return true; + } + + /** + * 获取文件信息 + * @param string $path 保存路径 + * @param string $savename 保存的文件名 + * @param boolean $replace 同名文件是否覆盖 + * @return mixed 保存状态,true-成功,false-失败 + */ + public function getInfo($name = '') + { + return isset($this->info[$name]) ? $this->info[$name] : $this->info; + } + +}