PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/ueditor/php/Uploader.class.php

https://github.com/ywl890227/longphp
PHP | 209 lines | 138 code | 17 blank | 54 comment | 16 complexity | de4c9e4217a511ed2da1cafe48a7c19e MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Created by JetBrains PhpStorm.
  4. * User: taoqili
  5. * Date: 12-7-18
  6. * Time: 上午11: 32
  7. * UEditor编辑器通用上传类
  8. */
  9. class Uploader
  10. {
  11. private $fileField; //文件域名
  12. private $file; //文件上传对象
  13. private $config; //配置信息
  14. private $oriName; //原始文件名
  15. private $fileName; //新文件名
  16. private $fullName; //完整文件名,即从当前配置目录开始的URL
  17. private $fileSize; //文件大小
  18. private $fileType; //文件类型
  19. private $stateInfo; //上传状态信息,
  20. private $stateMap = array( //上传状态映射表,国际化用户需考虑此处数据的国际化
  21. "SUCCESS" , //上传成功标记,在UEditor中内不可改变,否则flash判断会出错
  22. "文件大小超出 upload_max_filesize 限制" ,
  23. "文件大小超出 MAX_FILE_SIZE 限制" ,
  24. "文件未被完整上传" ,
  25. "没有文件被上传" ,
  26. "上传文件为空" ,
  27. "文件写入到临时文件夹出错" ,
  28. "POST" => "文件大小超出 post_max_size 限制" ,
  29. "SIZE" => "文件大小超出网站限制" ,
  30. "TYPE" => "不允许的文件类型" ,
  31. "DIR" => "目录创建失败" ,
  32. "IO" => "输入输出错误" ,
  33. "UNKNOWN" => "未知错误" ,
  34. "MOVE" => "文件保存时出错",
  35. "DIR_ERROR" => "创建目录失败"
  36. );
  37. /**
  38. * 构造函数
  39. * @param string $fileField 表单名称
  40. * @param array $config 配置项
  41. * @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名
  42. */
  43. public function __construct( $fileField , $config , $base64 = false )
  44. {
  45. $this->fileField = $fileField;
  46. $this->config = $config;
  47. $this->stateInfo = $this->stateMap[ 0 ];
  48. $this->upFile( $base64 );
  49. }
  50. /**
  51. * 上传文件的主处理方法
  52. * @param $base64
  53. * @return mixed
  54. */
  55. private function upFile( $base64 )
  56. {
  57. //处理base64上传
  58. if ( "base64" == $base64 ) {
  59. $content = $_POST[ $this->fileField ];
  60. $this->base64ToImage( $content );
  61. return;
  62. }
  63. //处理普通上传
  64. $file = $this->file = $_FILES[ $this->fileField ];
  65. if ( !$file ) {
  66. $this->stateInfo = $this->getStateInfo( 'POST' );
  67. return;
  68. }
  69. if ( $this->file[ 'error' ] ) {
  70. $this->stateInfo = $this->getStateInfo( $file[ 'error' ] );
  71. return;
  72. }
  73. if ( !is_uploaded_file( $file[ 'tmp_name' ] ) ) {
  74. $this->stateInfo = $this->getStateInfo( "UNKNOWN" );
  75. return;
  76. }
  77. $this->oriName = $file[ 'name' ];
  78. $this->fileSize = $file[ 'size' ];
  79. $this->fileType = $this->getFileExt();
  80. if ( !$this->checkSize() ) {
  81. $this->stateInfo = $this->getStateInfo( "SIZE" );
  82. return;
  83. }
  84. if ( !$this->checkType() ) {
  85. $this->stateInfo = $this->getStateInfo( "TYPE" );
  86. return;
  87. }
  88. $folder = $this->getFolder();
  89. if ( $folder === false ) {
  90. $this->stateInfo = $this->getStateInfo( "DIR_ERROR" );
  91. return;
  92. }
  93. $this->fullName = $folder . '/' . $this->getName();
  94. if ( $this->stateInfo == $this->stateMap[ 0 ] ) {
  95. if ( !move_uploaded_file( $file[ "tmp_name" ] , $this->fullName ) ) {
  96. $this->stateInfo = $this->getStateInfo( "MOVE" );
  97. }
  98. }
  99. }
  100. /**
  101. * 处理base64编码的图片上传
  102. * @param $base64Data
  103. * @return mixed
  104. */
  105. private function base64ToImage( $base64Data )
  106. {
  107. $img = base64_decode( $base64Data );
  108. $this->fileName = time() . rand( 1 , 10000 ) . ".png";
  109. $this->fullName = $this->getFolder() . '/' . $this->fileName;
  110. if ( !file_put_contents( $this->fullName , $img ) ) {
  111. $this->stateInfo = $this->getStateInfo( "IO" );
  112. return;
  113. }
  114. $this->oriName = "";
  115. $this->fileSize = strlen( $img );
  116. $this->fileType = ".png";
  117. }
  118. /**
  119. * 获取当前上传成功文件的各项信息
  120. * @return array
  121. */
  122. public function getFileInfo()
  123. {
  124. return array(
  125. "originalName" => $this->oriName ,
  126. "name" => $this->fileName ,
  127. "url" => $this->fullName ,
  128. "size" => $this->fileSize ,
  129. "type" => $this->fileType ,
  130. "state" => $this->stateInfo
  131. );
  132. }
  133. /**
  134. * 上传错误检查
  135. * @param $errCode
  136. * @return string
  137. */
  138. private function getStateInfo( $errCode )
  139. {
  140. return !$this->stateMap[ $errCode ] ? $this->stateMap[ "UNKNOWN" ] : $this->stateMap[ $errCode ];
  141. }
  142. /**
  143. * 重命名文件
  144. * @return string
  145. */
  146. private function getName()
  147. {
  148. return $this->fileName = time() . rand( 1 , 10000 ) . $this->getFileExt();
  149. }
  150. /**
  151. * 文件类型检测
  152. * @return bool
  153. */
  154. private function checkType()
  155. {
  156. return in_array( $this->getFileExt() , $this->config[ "allowFiles" ] );
  157. }
  158. /**
  159. * 文件大小检测
  160. * @return bool
  161. */
  162. private function checkSize()
  163. {
  164. return $this->fileSize <= ( $this->config[ "maxSize" ] * 1024 );
  165. }
  166. /**
  167. * 获取文件扩展名
  168. * @return string
  169. */
  170. private function getFileExt()
  171. {
  172. return strtolower( strrchr( $this->file[ "name" ] , '.' ) );
  173. }
  174. /**
  175. * 按照日期自动创建存储文件夹
  176. * @return string
  177. */
  178. private function getFolder()
  179. {
  180. $pathStr = $this->config[ "savePath" ];
  181. if ( strrchr( $pathStr , "/" ) != "/" ) {
  182. $pathStr .= "/";
  183. }
  184. $pathStr .= date( "Ymd" );
  185. if ( !file_exists( $pathStr ) ) {
  186. if ( !mkdir( $pathStr , 0777 , true ) ) {
  187. return false;
  188. }
  189. }
  190. return $pathStr;
  191. }
  192. }