内核更新
This commit is contained in:
@@ -23,6 +23,8 @@ class File extends SplFileObject
|
||||
private $error = '';
|
||||
// 当前完整文件名
|
||||
protected $filename;
|
||||
// 上传文件名
|
||||
protected $saveName;
|
||||
// 文件上传命名规则
|
||||
protected $rule = 'date';
|
||||
// 文件上传验证规则
|
||||
@@ -31,6 +33,8 @@ class File extends SplFileObject
|
||||
protected $isTest;
|
||||
// 上传文件信息
|
||||
protected $info;
|
||||
// 文件hash信息
|
||||
protected $hash = [];
|
||||
|
||||
public function __construct($filename, $mode = 'r')
|
||||
{
|
||||
@@ -70,6 +74,50 @@ class File extends SplFileObject
|
||||
return isset($this->info[$name]) ? $this->info[$name] : $this->info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上传文件的文件名
|
||||
* @return string
|
||||
*/
|
||||
public function getSaveName()
|
||||
{
|
||||
return $this->saveName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上传文件的保存文件名
|
||||
* @param string $saveName
|
||||
* @return $this
|
||||
*/
|
||||
public function setSaveName($saveName)
|
||||
{
|
||||
$this->saveName = $saveName;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件的md5散列值
|
||||
* @return $this
|
||||
*/
|
||||
public function md5()
|
||||
{
|
||||
if (!isset($this->hash['md5'])) {
|
||||
$this->hash['md5'] = md5_file($this->filename);
|
||||
}
|
||||
return $this->hash['md5'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件的sha1散列值
|
||||
* @return $this
|
||||
*/
|
||||
public function sha1()
|
||||
{
|
||||
if (!isset($this->hash['sha1'])) {
|
||||
$this->hash['sha1'] = sha1_file($this->filename);
|
||||
}
|
||||
return $this->hash['sha1'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查目录是否可写
|
||||
* @param string $path 目录
|
||||
@@ -183,7 +231,6 @@ class File extends SplFileObject
|
||||
if (!in_array($extension, $ext)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -250,6 +297,12 @@ class File extends SplFileObject
|
||||
*/
|
||||
public function move($path, $savename = true, $replace = true)
|
||||
{
|
||||
// 文件上传失败,捕获错误代码
|
||||
if (!empty($this->info['error'])) {
|
||||
$this->error($this->info['error']);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 检测合法性
|
||||
if (!$this->isValid()) {
|
||||
$this->error = '非法上传文件';
|
||||
@@ -262,28 +315,32 @@ class File extends SplFileObject
|
||||
}
|
||||
$path = rtrim($path, DS) . DS;
|
||||
// 文件保存命名规则
|
||||
$savename = $this->getSaveName($savename);
|
||||
$saveName = $this->buildSaveName($savename);
|
||||
$filename = $path . $saveName;
|
||||
|
||||
// 检测目录
|
||||
if (false === $this->checkPath(dirname($path . $savename))) {
|
||||
if (false === $this->checkPath(dirname($filename))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 不覆盖同名文件 */
|
||||
if (!$replace && is_file($path . $savename)) {
|
||||
$this->error = '存在同名文件' . $path . $savename;
|
||||
if (!$replace && is_file($filename)) {
|
||||
$this->error = '存在同名文件' . $filename;
|
||||
return false;
|
||||
}
|
||||
|
||||
/* 移动文件 */
|
||||
if ($this->isTest) {
|
||||
rename($this->filename, $path . $savename);
|
||||
} elseif (!move_uploaded_file($this->filename, $path . $savename)) {
|
||||
rename($this->filename, $filename);
|
||||
} elseif (!move_uploaded_file($this->filename, $filename)) {
|
||||
$this->error = '文件上传保存错误!';
|
||||
return false;
|
||||
}
|
||||
|
||||
return new SplFileInfo($path . $savename);
|
||||
// 返回 File对象实例
|
||||
$file = new self($filename);
|
||||
$file->setSaveName($saveName);
|
||||
$file->setUploadInfo($this->info);
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -291,7 +348,7 @@ class File extends SplFileObject
|
||||
* @param string|bool $savename 保存的文件名 默认自动生成
|
||||
* @return string
|
||||
*/
|
||||
protected function getSaveName($savename)
|
||||
protected function buildSaveName($savename)
|
||||
{
|
||||
if (true === $savename) {
|
||||
// 自动生成文件名
|
||||
@@ -323,6 +380,34 @@ class File extends SplFileObject
|
||||
return $savename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误代码信息
|
||||
* @param int $errorNo 错误号
|
||||
*/
|
||||
private function error($errorNo)
|
||||
{
|
||||
switch ($errorNo) {
|
||||
case 1:
|
||||
case 2:
|
||||
$this->error = '上传文件大小超过了最大值!';
|
||||
break;
|
||||
case 3:
|
||||
$this->error = '文件只有部分被上传!';
|
||||
break;
|
||||
case 4:
|
||||
$this->error = '没有文件被上传!';
|
||||
break;
|
||||
case 6:
|
||||
$this->error = '找不到临时文件夹!';
|
||||
break;
|
||||
case 7:
|
||||
$this->error = '文件写入失败!';
|
||||
break;
|
||||
default:
|
||||
$this->error = '未知上传错误!';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误信息
|
||||
* @return mixed
|
||||
|
||||
Reference in New Issue
Block a user