test_service d3170b4d1c 1
2023-12-01 15:43:29 +08:00

63 lines
1.9 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace hema\storage\engine;
/**
* 本地文件驱动
*/
class Local extends Basics
{
/**
* 上传图片文件
* @return array|bool
*/
public function upload()
{
// 验证文件类型
$saveFileInfo = $this->getSaveFileInfo();
$value = get_addons_config('upload');
if($value['file_ext']){
$file_ext = explode(',',$value['file_ext']);
}else{
$file_ext = ['jpg','jpeg','png','bmp','gif'];
}
// 文件扩展名
if(!in_array($saveFileInfo['file_ext'], $file_ext)){
$this->error = '上传图片类型只能为:' . $value['file_ext'] .'格式';
return false;
}
if($value['file_size']){
$file_size = (int)$value['file_size'];
}else{
$file_size = 2;//默认值
}
// 文件大小(字节)不能大于2M1024*1024 = 1048576
if($saveFileInfo['file_size'] > ($file_size * 1048576)){
$this->error = '上传图片大于' . (string)$file_size . 'M';
return false;
}
try {
$fileInfo = pathinfo($this->file);
$savePath = root_path() . 'public/uploads/'.$saveFileInfo['folder'];
$info = $this->file->move($savePath);
//修改文件名称
rename($savePath . DIRECTORY_SEPARATOR . $fileInfo['basename'],$savePath . DIRECTORY_SEPARATOR . $saveFileInfo['hash_name']);
return $saveFileInfo;
} catch (\Exception $e) {
$this->error = $e->getMessage();
return false;
}
}
/**
* 删除文件
* @param string $filePath
* @return bool|mixed
*/
public function delete(string $filePath)
{
// 文件所在目录
$realPath = realpath(web_path() . "uploads/{$filePath}");
return $realPath === false || unlink($realPath);
}
}