vr-shopxo-source/extend/base/Uploader.php

517 lines
17 KiB
PHP
Raw Normal View History

2018-12-28 10:58:37 +00:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 02:34:52 +00:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2018-12-28 10:58:37 +00:00
// +----------------------------------------------------------------------
2021-03-16 02:34:52 +00:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2018-12-28 10:58:37 +00:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace base;
/**
* Created by JetBrains PhpStorm.
* User: taoqili
* Date: 12-7-18
* Time: 上午11: 32
* UEditor编辑器通用上传类
*/
class Uploader
{
private $fileField; //文件域名
private $file; //文件上传对象
2024-04-15 02:25:01 +00:00
private $type; //类型
2018-12-28 10:58:37 +00:00
private $base64; //文件上传对象
private $config; //配置信息
private $oriName; //原始文件名
private $fileName; //新文件名
private $fullName; //完整文件名,即从当前配置目录开始的URL
private $filePath; //完整文件名,即从当前配置目录开始的URL
private $fileSize; //文件大小
private $fileType; //文件类型
private $stateInfo; //上传状态信息,
/**
* 构造函数
* @param string $fileField 表单名称
* @param array $config 配置项
* @param bool $base64 是否解析base64编码可省略。若开启$fileField代表的是base64编码的字符串表单名
*/
2023-02-12 14:14:47 +00:00
public function __construct($fileField, $config, $type = 'file')
2018-12-28 10:58:37 +00:00
{
$this->fileField = $fileField;
$this->config = $config;
$this->type = $type;
2019-02-26 06:04:27 +00:00
switch($this->type)
{
// 抓取远程文件
case 'remote' :
$this->saveRemote();
break;
// base64文件
case 'base64' :
2019-06-25 16:13:47 +00:00
case 'scrawl' :
2019-02-26 06:04:27 +00:00
$this->uploadBase64();
break;
// 图片
case 'image' :
$this->uploadImage();
break;
// 文件、视频
case 'file' :
case 'video' :
$this->uploadFile();
break;
// 默认
default :
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_upload_type');
2019-02-26 06:04:27 +00:00
}
}
/**
* 文件上传
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-02-26
* @desc description
*/
private function uploadFile()
{
2019-06-26 10:38:06 +00:00
if(empty($_FILES[$this->fileField]))
{
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_size_exceed');
2019-06-26 10:38:06 +00:00
return;
}
2023-11-08 07:43:08 +00:00
$this->file = $_FILES[$this->fileField];
if (!$this->file) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_file_not_found');
2019-02-26 06:04:27 +00:00
return;
}
if ($this->file['error']) {
2023-11-08 07:43:08 +00:00
$this->stateInfo = $this->getFileErrorInfo($this->file['error']);
2019-02-26 06:04:27 +00:00
return;
2023-11-08 07:43:08 +00:00
} else if (!file_exists($this->file['tmp_name'])) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_tmp_file_not_found');
2019-02-26 06:04:27 +00:00
return;
2023-11-08 07:43:08 +00:00
} else if (!is_uploaded_file($this->file['tmp_name'])) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_tmp_file');
2019-02-26 06:04:27 +00:00
return;
}
2023-11-08 07:43:08 +00:00
$this->oriName = $this->file['name'];
$this->fileSize = $this->file['size'];
2019-02-26 06:04:27 +00:00
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);
//检查文件大小是否超出限制
if (!$this->checkSize()) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_size_exceed');
2019-02-26 06:04:27 +00:00
return;
}
//检查是否不允许的文件格式
if (!$this->checkType()) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_type_not_allowed');
2019-02-26 06:04:27 +00:00
return;
}
//创建目录失败
if (!is_dir($dirname) && !@mkdir($dirname, 0777, true)) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_create_dir');
2019-02-26 06:04:27 +00:00
return;
} else if (!is_writeable($dirname)) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_dir_not_writeable');
2019-02-26 06:04:27 +00:00
return;
}
2025-03-12 12:57:10 +00:00
// 安全验证
$ret = \base\FileUtil::FileContentSecurityCheck($this->file['tmp_name']);
if($ret['code'] != 0)
{
$this->stateInfo = $ret['msg'];
return;
}
2019-02-26 06:04:27 +00:00
//移动文件
2023-11-08 07:43:08 +00:00
if (!(move_uploaded_file($this->file['tmp_name'], $this->filePath) && file_exists($this->filePath))) { //移动失败
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_file_move');
2019-02-26 06:04:27 +00:00
} else { //移动成功
2023-03-01 04:09:04 +00:00
$this->stateInfo = 'SUCCESS';
2018-12-28 10:58:37 +00:00
}
}
/**
2019-02-26 06:04:27 +00:00
* 图片上传
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2019-02-26
* @desc description
2018-12-28 10:58:37 +00:00
*/
2019-02-26 06:04:27 +00:00
private function uploadImage()
2018-12-28 10:58:37 +00:00
{
2023-11-08 07:43:08 +00:00
$this->file = empty($_FILES[$this->fileField]) ? '' : $_FILES[$this->fileField];
if (!$this->file) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_file_not_found');
2018-12-28 10:58:37 +00:00
return;
}
if ($this->file['error']) {
2023-11-08 07:43:08 +00:00
$this->stateInfo = $this->getStateErrorInfo($this->file['error']);
2018-12-28 10:58:37 +00:00
return;
2023-11-08 07:43:08 +00:00
} else if (!file_exists($this->file['tmp_name'])) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_tmp_file_not_found');
2018-12-28 10:58:37 +00:00
return;
2023-11-08 07:43:08 +00:00
} else if (!is_uploaded_file($this->file['tmp_name'])) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_tmp_file');
2018-12-28 10:58:37 +00:00
return;
}
2019-10-07 14:13:52 +00:00
// 防止原名称没有带后缀
2023-11-08 07:43:08 +00:00
$info = getimagesize($this->file['tmp_name']);
if(stripos($this->file['name'], '.') === false)
2019-10-07 14:13:52 +00:00
{
2023-11-08 07:43:08 +00:00
$this->file['name'] .= str_replace('/', '.', $info['mime']);
2019-10-07 14:13:52 +00:00
}
2023-11-08 07:43:08 +00:00
$this->oriName = $this->file['name'];
$this->fileSize = $this->file['size'];
2018-12-28 10:58:37 +00:00
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);
//检查文件大小是否超出限制
if (!$this->checkSize()) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_size_exceed');
2018-12-28 10:58:37 +00:00
return;
}
//检查是否不允许的文件格式
if (!$this->checkType()) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_type_not_allowed');
2018-12-28 10:58:37 +00:00
return;
}
//创建目录失败
if (!is_dir($dirname) && !@mkdir($dirname, 0777, true)) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_create_dir');
2018-12-28 10:58:37 +00:00
return;
} else if (!is_writeable($dirname)) {
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_dir_not_writeable');
2018-12-28 10:58:37 +00:00
return;
}
2025-03-12 12:57:10 +00:00
// 是否php文件类型
if($info['mime'] == 'text/x-php')
2021-08-21 03:14:42 +00:00
{
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('invalid_file');
2021-08-21 03:14:42 +00:00
return;
}
2025-03-12 12:57:10 +00:00
// 安全验证
$ret = \base\FileUtil::FileContentSecurityCheck($this->file['tmp_name']);
if($ret['code'] != 0)
{
$this->stateInfo = $ret['msg'];
return;
}
2020-12-05 12:35:13 +00:00
// 是否需要直接存储文件
2023-11-08 07:43:08 +00:00
if(!move_uploaded_file($this->file['tmp_name'], $this->filePath))
2020-12-05 12:35:13 +00:00
{
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_file_move');
2020-12-05 12:35:13 +00:00
}
// 图片是否存储成功
if(!file_exists($this->filePath))
{
2023-02-12 14:14:47 +00:00
$this->stateInfo = $this->getStateErrorInfo('error_image_save');
} else {
2023-03-01 04:09:04 +00:00
$this->stateInfo = 'SUCCESS';
2018-12-28 10:58:37 +00:00
}
}
/**