mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改进File类
This commit is contained in:
@@ -20,6 +20,8 @@ class File extends SplFileObject
|
||||
* @var string
|
||||
*/
|
||||
private $error = '';
|
||||
// 当前完整文件名
|
||||
protected $filename;
|
||||
// 文件上传命名规则
|
||||
protected $rule = 'date';
|
||||
// 单元测试
|
||||
@@ -28,11 +30,12 @@ class File extends SplFileObject
|
||||
// 上传文件信息
|
||||
protected $info;
|
||||
|
||||
public function __construct($filename, $info = [],$isTest=false)
|
||||
public function __construct($filename, $info = [], $isTest = false)
|
||||
{
|
||||
parent::__construct($filename);
|
||||
$this->info = $info;
|
||||
$this->isTest = $isTest;
|
||||
$this->filename = $this->getRealPath();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,7 +74,7 @@ class File extends SplFileObject
|
||||
public function getMime()
|
||||
{
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
return finfo_file($finfo, $this->getRealPath());
|
||||
return finfo_file($finfo, $this->filename);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,9 +95,9 @@ class File extends SplFileObject
|
||||
public function isValid()
|
||||
{
|
||||
if($this->isTest){
|
||||
return is_file($this->getRealPath());
|
||||
return is_file($this->filename);
|
||||
}
|
||||
return is_uploaded_file($this->getRealPath());
|
||||
return is_uploaded_file($this->filename);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,8 +132,8 @@ class File extends SplFileObject
|
||||
|
||||
/* 移动文件 */
|
||||
if($this->isTest){
|
||||
rename($this->getRealPath(),$path.$savename);
|
||||
} elseif (!move_uploaded_file($this->getRealPath(), $path . $savename)) {
|
||||
rename($this->filename, $path.$savename);
|
||||
} elseif (!move_uploaded_file($this->filename, $path . $savename)) {
|
||||
$this->error = '文件上传保存错误!';
|
||||
return false;
|
||||
}
|
||||
@@ -152,11 +155,11 @@ class File extends SplFileObject
|
||||
} else {
|
||||
switch ($this->rule) {
|
||||
case 'md5':
|
||||
$md5 = md5_file($this->getRealPath());
|
||||
$md5 = md5_file($this->filename);
|
||||
$savename = substr($md5, 0, 2) . DS . substr($md5, 2);
|
||||
break;
|
||||
case 'sha1':
|
||||
$sha1 = sha1_file($this->getRealPath());
|
||||
$sha1 = sha1_file($this->filename);
|
||||
$savename = substr($sha1, 0, 2) . DS . substr($sha1, 2);
|
||||
break;
|
||||
case 'date':
|
||||
|
||||
Reference in New Issue
Block a user