File类改进

This commit is contained in:
thinkphp
2016-07-23 20:57:54 +08:00
parent 253ccaaad0
commit 587bdcde2a

View File

@@ -23,6 +23,8 @@ class File extends SplFileObject
private $error = ''; private $error = '';
// 当前完整文件名 // 当前完整文件名
protected $filename; protected $filename;
// 上传文件名
protected $saveName;
// 文件上传命名规则 // 文件上传命名规则
protected $rule = 'date'; protected $rule = 'date';
// 文件上传验证规则 // 文件上传验证规则
@@ -70,6 +72,15 @@ class File extends SplFileObject
return isset($this->info[$name]) ? $this->info[$name] : $this->info; return isset($this->info[$name]) ? $this->info[$name] : $this->info;
} }
/**
* 获取上传文件的文件名
* @return string
*/
public function saveName()
{
return $this->saveName;
}
/** /**
* 检查目录是否可写 * 检查目录是否可写
* @param string $path 目录 * @param string $path 目录
@@ -262,28 +273,29 @@ class File extends SplFileObject
} }
$path = rtrim($path, DS) . DS; $path = rtrim($path, DS) . DS;
// 文件保存命名规则 // 文件保存命名规则
$savename = $this->getSaveName($savename); $this->saveName = $this->getSaveName($savename);
$filename = $path . $this->saveName;
// 检测目录 // 检测目录
if (false === $this->checkPath(dirname($path . $savename))) { if (false === $this->checkPath(dirname($filename))) {
return false; return false;
} }
/* 不覆盖同名文件 */ /* 不覆盖同名文件 */
if (!$replace && is_file($path . $savename)) { if (!$replace && is_file($filename)) {
$this->error = '存在同名文件' . $path . $savename; $this->error = '存在同名文件' . $filename;
return false; return false;
} }
/* 移动文件 */ /* 移动文件 */
if ($this->isTest) { if ($this->isTest) {
rename($this->filename, $path . $savename); rename($this->filename, $filename);
} elseif (!move_uploaded_file($this->filename, $path . $savename)) { } elseif (!move_uploaded_file($this->filename, $filename)) {
$this->error = '文件上传保存错误!'; $this->error = '文件上传保存错误!';
return false; return false;
} }
return new SplFileInfo($path . $savename); return new SplFileInfo($filename);
} }
/** /**