diff --git a/library/think/File.php b/library/think/File.php index 89c7b248..e30a1c50 100644 --- a/library/think/File.php +++ b/library/think/File.php @@ -15,12 +15,13 @@ use SplFileObject; class File extends SplFileObject { - /** * 错误信息 * @var string */ private $error = ''; + // 文件上传命名规则 + protected $rule = 'date'; /** * 检查目录是否可写 @@ -51,24 +52,49 @@ class File extends SplFileObject return finfo_file($finfo, $this->getRealPath()); } + /** + * 设置文件的命名规则 + * @param string $rule 文件命名规则 + * @return $this + */ + public function rule($rule) + { + $this->rule = $rule; + return $this; + } + + /** + * 检测是否合法的上传文件 + * @return bool + */ + public function isValid() + { + return is_uploaded_file($this->getRealPath()); + } + /** * 移动文件 * @param string $path 保存路径 - * @param string $savename 保存的文件名 + * @param string|bool $savename 保存的文件名 默认自动生成 * @param boolean $replace 同名文件是否覆盖 * @return false|SplFileInfo false-失败 否则返回SplFileInfo实例 */ - public function move($path, $savename = '', $replace = true) + public function move($path, $savename = true, $replace = true) { - if (!is_uploaded_file($this->getRealPath())) { + // 检测合法性 + if (!$this->isValid()) { + $this->error = '非法上传文件'; return false; } - if (false === $this->checkPath($path)) { + // 文件保存命名规则 + $savename = $this->getSaveName($savename); + + // 检测目录 + if (false === $this->checkPath(dirname($path . $savname))) { return false; } - $savename = $savename ?: $this->getFilename(); /* 不覆盖同名文件 */ if (!$replace && is_file($path . $savename)) { $this->error = '存在同名文件' . $path . $savename; @@ -84,6 +110,41 @@ class File extends SplFileObject return new \SplFileInfo($path . $savename); } + /** + * 获取保存文件名 + * @param string|bool $savename 保存的文件名 默认自动生成 + * @return string + */ + protected function getSaveName($savename) + { + if (true === $savename) { + // 自动生成文件名 + if ($this->rule instanceof \Closure) { + $savename = call_user_func_array($this->rule, [$this->getFilename()]); + } else { + switch ($this->rule) { + case 'uniqid': + $savename = uniqid(); + break; + case 'md5': + $savename = md5($this->getFilename()); + break; + case 'date': + $savename = date('Y-m-d') . DS . md5(microtime(true)); + break; + default: + $savename = call_user_func($this->rule); + } + } + if (!strpos($savename, '.')) { + $savename .= '.' . $this->getExtension(); + } + } elseif ('' === $savename) { + $savename = $this->getFilename(); + } + return $savename; + } + /** * 获取错误信息 * @return mixed