<?php
/**
* Created by JetBrains PhpStorm.
* User: taoqili
* Date: 12-7-18
* Time: 上午11: 32
* UEditor编辑器通用上传类
*/
class Uploader
{
private $fileField; //文件域名
private $file; //文件上传对象
private $config; //配置信息
private $oriName; //原始文件名
private $fileName; //新文件名
private $fullName; //完整文件名,即从当前配置目录开始的URL
private $fileSize; //文件大小
private $fileType; //文件类型
private $stateInfo; //上传状态信息,
private $stateMap = array( //上传状态映射表,国际化用户需考虑此处数据的国际化
"SUCCESS" , //上传成功标记,在UEditor中内不可改变,否则flash判断会出错
"文件大小超出 upload_max_filesize 限制" ,
"文件大小超出 MAX_FILE_SIZE 限制" ,
"文件未被完整上传" ,
"没有文件被上传" ,
"上传文件为空" ,
"POST" => "文件大小超出 post_max_size 限制" ,
"SIZE" => "文件大小超出网站限制" ,
"TYPE" => "不允许的文件类型" ,
"DIR" => "目录创建失败" ,
"IO" => "输入输出错误" ,
"UNKNOWN" => "未知错误" ,
"MOVE" => "文件保存时出错"
);
/**
* 构造函数
* @param string $fileField 表单名称
* @param array $config 配置项
* @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名
*/
public function __construct( $fileField , $config , $base64 = false )
{
$this->fileField = $fileField;
$this->config = $config;
$this->stateInfo = $this->stateMap[ 0 ];
$this->upFile( $base64 );
}
/**
* 上传文件的主处理方法
* @param $base64
* @return mixed
*/
private function upFile( $base64 )
{
//处理base64上传
if ( "base64" == $base64 ) {
$content = $_POST[ $this->fileField ];
$this->base64ToImage( $content );
return;
}
//处理普通上传
$file = $this->file = $_FILES[ $this->fileField ];
if ( !$file ) {
$this->stateInfo = $this->getStateInfo( 'POST' );
return;
}
if ( $this->file[ 'error' ] ) {
$this->stateInfo = $this->getStateInfo( $file[ 'error' ] );
return;
}
if ( !is_uploaded_file( $file[ 'tmp_name' ] ) ) {
$this->stateInfo = $this->getStateInfo( "UNKNOWN" );
return;
}
$this->oriName = $file[ 'name' ];
$this->fileSize = $file[ 'size' ];
$this->fileType = $this->getFileExt();
if ( !$this->checkSize() ) {
$this->stateInfo = $this->getStateInfo( "SIZE" );
return;
}
if ( !$this->checkType() ) {
$this->stateInfo = $this->getStateInfo( "TYPE" );
return;
}
$this->fullName = $this->getFolder() . '/' . $this->getName();
if ( $this->stateInfo == $this->stateMap[ 0 ] ) {
if ( !move_uploaded_file( $file[ "tmp_name" ] , $this->fullName ) ) {
$this->stateInfo = $this->getStateInfo( "MOVE" );
}
}
}
/**
* 处理base64编码的图片上传
* @param $base64Data
* @return mixed
*/
private function base64ToImage( $base64Data )
{
$img = base64_decode( $base64Data );
$this->fileName = time() . rand( 1 , 10000 ) . ".png";
$this->fullName = $this->getFolder() . '/' . $this->fileName;
if ( !file_put_contents( $this->fullName , $img ) ) {
$this->stateInfo = $this->getStateInfo( "IO" );
return;
}
$this->oriName = "";
$this->fileSize = strlen( $img );
$this->fileType = ".png";
}
/**
* 获取当前上传成功文件的各项信息
* @return array
*/
public function getFileInfo()
{
return array(
"originalName" => $this->oriName ,
"name" => $this->fileName ,
"url" => $this->fullName ,
"size" => $this->fileSize ,
"type" => $this->fileType ,
"state" => $this->stateInfo
);
}
/**
* 上传错误检查
* @param $errCode
* @return string
*/
private function getStateInfo( $errCode )
{
return !$this->stateMap[ $errCode ] ? $this->stateMap[ "UNKNOWN" ] : $this->stateMap[ $errCode ];
}
/**
* 重命名文件
* @return string
*/
private function getName()
{
return $this->fileName = time() . rand( 1 , 10000 ) . $this->getFileExt();
}
/**
* 文件类型检测
* @return bool
*/
private function checkType()
{
return in_array( $this->getFileExt() , $this->config[ "allowFiles" ] );
}
/**
* 文件大小检测
* @return bool
*/
private function checkSize()
{
return $this->fileSize <= ( $this->config[ "maxSize" ] * 1024 );
}
/**
* 获取文件扩展名
* @return string
*/
private function getFileExt()
{
return strtolower( strrchr( $this->file[ "name" ] , '.' ) );
}
/**
* 按照日期自动创建存储文件夹
* @return string
*/
private function getFolder()
{
$pathStr = $this->config[ "savePath" ];
if ( strrchr( $pathStr , "/" ) != "/" ) {
$pathStr .= "/";
}
$pathStr .= date( "Ymd" );
if ( !file_exists( $pathStr ) ) {
if ( !mkdir( $pathStr , 0777 , true ) ) {
return false;
}
}
return $pathStr;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于PHP的UEditor
共187个文件
png:60个
gif:34个
js:29个
需积分: 50 24 下载量 59 浏览量
2013-07-15
00:49:03
上传
评论
收藏 1.77MB RAR 举报
温馨提示
部署在PHP的UEditor,关键点还在路径配置上,有什么问题,可以和我交流。QQ:70747053.博客地址:http://blog.csdn.net/leamonlm
资源推荐
资源详情
资源评论
收起资源包目录
基于PHP的UEditor (187个子文件)
index.php.bak 545B
ueditor.css 42KB
shCoreDefault.css 7KB
scrawl.css 4KB
codemirror.css 3KB
image.css 3KB
attachment.css 3KB
video.css 2KB
emotion.css 2KB
dialogbase.css 2KB
music.css 2KB
background.css 1KB
edittable.css 1KB
template.css 1KB
help.css 395B
iframe.css 42B
UEditorSnapscreen.exe 508KB
wface.gif 49KB
jxface2.gif 40KB
yface.gif 28KB
bface.gif 27KB
icons.gif 21KB
tface.gif 19KB
fface.gif 18KB
cface.gif 8KB
icons-all.gif 4KB
videologo.gif 2KB
cancelbutton.gif 1KB
button-bg.gif 1KB
lock.gif 1KB
word.gif 1019B
icon_doc.gif 1012B
icon_psd.gif 1009B
icon_rar.gif 1007B
icon_xls.gif 1005B
icon_mv.gif 1001B
icon_ppt.gif 1001B
icon_pdf.gif 996B
icon_mp3.gif 986B
icon_txt.gif 970B
icon_exe.gif 949B
icon_chm.gif 923B
cursor_v.gif 370B
cursor_h.gif 253B
anchor.gif 184B
unhighlighted.gif 111B
highlighted.gif 111B
bg.gif 84B
pagebreak.gif 54B
0.gif 43B
spacer.gif 43B
image.html 11KB
attachment.html 6KB
wordimage.html 6KB
emotion.html 6KB
highlightcode.html 5KB
link.html 4KB
insertframe.html 4KB
map.html 4KB
gmap.html 4KB
scrawl.html 4KB
searchreplace.html 3KB
video.html 3KB
help.html 3KB
background.html 3KB
webapp.html 2KB
edittable.html 2KB
snapscreen.html 2KB
anchor.html 2KB
edittd.html 2KB
music.html 981B
template.html 948B
edittip.html 863B
spechars.html 849B
13737979771972.jpg 76KB
13738007404204.jpg 58KB
center_focus.jpg 12KB
center_focus.jpg 12KB
none_focus.jpg 11KB
none_focus.jpg 11KB
left_focus.jpg 11KB
left_focus.jpg 11KB
right_focus.jpg 11KB
right_focus.jpg 11KB
ueditor.all.js 827KB
codemirror.js 158KB
shCore.js 156KB
image.js 107KB
swfupload.js 54KB
tangram.js 41KB
scrawl.js 26KB
en.js 24KB
ueditor.config.js 24KB
zh-cn.js 22KB
video.js 13KB
swfupload.speed.js 12KB
config.js 12KB
background.js 12KB
music.js 8KB
edittable.js 7KB
共 187 条
- 1
- 2
资源评论
农妇山泉有点田
- 粉丝: 2
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功