mirror of
https://gitee.com/zongzhige/shopxo.git
synced 2024-12-02 03:48:47 +08:00
159 lines
4.9 KiB
PHP
Executable File
159 lines
4.9 KiB
PHP
Executable File
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | ShopXO 国内领先企业级B2C免费开源电商系统
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Licensed ( https://opensource.org/licenses/mit-license.php )
|
|
// +----------------------------------------------------------------------
|
|
// | Author: Devil
|
|
// +----------------------------------------------------------------------
|
|
namespace base;
|
|
|
|
/**
|
|
* 文件上传
|
|
* @author Devil
|
|
* @blog http://gong.gg/
|
|
* @version 1.0.0
|
|
* @date 2018-06-28
|
|
* @desc 支持所有文件存储到硬盘
|
|
*/
|
|
class FileUpload
|
|
{
|
|
// 配置
|
|
private $config;
|
|
|
|
/**
|
|
* 构造方法
|
|
* @author Devil
|
|
* @blog http://gong.gg/
|
|
* @version 1.0.0
|
|
* @date 2018-10-19
|
|
* @desc description
|
|
* @param [array] $params [输入参数]
|
|
*/
|
|
public function __construct($params = [])
|
|
{
|
|
$this->config['dir'] = isset($params['dir']) ? $params['dir'] : ROOT.'public';
|
|
$this->config['path'] = isset($params['path']) ? $params['path'] : DS.'static'.DS.'upload'.DS.'file'.DS.date('Y').DS.date('m').DS.date('d').DS;
|
|
}
|
|
|
|
/**
|
|
* 文件存储
|
|
* @author Devil
|
|
* @blog http://gong.gg/
|
|
* @version 1.0.0
|
|
* @date 2018-06-29
|
|
* @desc description
|
|
* @param [string] $file [表单name]
|
|
* @param [string] $name [文件名称]
|
|
* @param [int] $index [多文件索引]
|
|
* @param [array] $params [输入参数]
|
|
* @return [mixed] [array | 错误信息]
|
|
*/
|
|
function Save($file, $name = '', $index = false, $params = [])
|
|
{
|
|
// 基础校验
|
|
$error = FileUploadError($file, $index);
|
|
if($error !== true)
|
|
{
|
|
return DataReturn($error, -1);
|
|
}
|
|
|
|
// 存储目录校验
|
|
$dir = str_replace(['//', '\\\\'], ['/', '\\'], $this->config['dir'].$this->config['path']);
|
|
$this->IsMkdir($dir);
|
|
|
|
// 临时文件数据
|
|
if($index === false)
|
|
{
|
|
$original_name = $_FILES[$file]['name'];
|
|
$temp_file = $_FILES[$file]['tmp_name'];
|
|
$size = $_FILES[$file]['size'];
|
|
$type = $_FILES[$file]['type'];
|
|
} else {
|
|
$original_name = $_FILES[$file]['name'][$index];
|
|
$temp_file = $_FILES[$file]['tmp_name'][$index];
|
|
$size = $_FILES[$file]['size'][$index];
|
|
$type = $_FILES[$file]['type'][$index];
|
|
}
|
|
|
|
$info = getimagesize($temp_file);
|
|
if(stripos($original_name, '.') === false)
|
|
{
|
|
$original_name .= str_replace('/', '.', $info['mime']);
|
|
}
|
|
|
|
// 图片文件
|
|
if(isset($params['data_type']) && $params['data_type'] == 'images')
|
|
{
|
|
// 验证一句话木马(如果是加密的无法判断)
|
|
$content = @file_get_contents($temp_file);
|
|
if(false == $content || preg_match('#<\?php#i', $content) || $info['mime'] == 'text/x-php')
|
|
{
|
|
return DataReturn('非法文件', -1);
|
|
}
|
|
}
|
|
|
|
// 后缀名称
|
|
$ext_all = explode('.', $original_name);
|
|
$ext = $ext_all[count($ext_all)-1];
|
|
|
|
// 文件名称,未指定则生成新的文件名称
|
|
$filename = empty($name) ? $this->RandNewFilename() : $name;
|
|
if(stripos($filename, '.') === false)
|
|
{
|
|
$filename .= '.'.$ext;
|
|
}
|
|
|
|
// 存储
|
|
if(move_uploaded_file($temp_file, $dir.$filename))
|
|
{
|
|
$data = [
|
|
'title' => $original_name,
|
|
'url' => $this->config['path'].$filename,
|
|
'path' => $this->config['path'],
|
|
'name' => $filename,
|
|
'ext' => $ext,
|
|
'size' => $size,
|
|
'type' => $type,
|
|
'hash' => hash_file('sha256', $dir.$filename, false),
|
|
'md5' => md5_file($dir.$filename),
|
|
];
|
|
return DataReturn('上传成功', 0, $data);
|
|
}
|
|
return DataReturn('文件存储失败', -1);
|
|
}
|
|
|
|
/**
|
|
* 路径不存在则创建
|
|
* @author Devil
|
|
* @blog http://gong.gg/
|
|
* @version 1.0.0
|
|
* @date 2018-06-29
|
|
* @desc description
|
|
* @param [string] $dir [文件路径]
|
|
*/
|
|
private function IsMkdir($dir)
|
|
{
|
|
if(!is_dir($dir))
|
|
{
|
|
mkdir($dir, 0777, true);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 生成新的文件名称
|
|
* @author Devil
|
|
* @blog http://gong.gg/
|
|
* @version 1.0.0
|
|
* @date 2018-06-29
|
|
* @desc description
|
|
* @return [string] [文件名称]
|
|
*/
|
|
private function RandNewFilename()
|
|
{
|
|
return date('YmdHis').GetNumberCode();
|
|
}
|
|
}
|
|
?>
|