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