Uploader.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. "POST" => "文件大小超出 post_max_size 限制" ,
  28. "SIZE" => "文件大小超出网站限制" ,
  29. "TYPE" => "不允许的文件类型" ,
  30. "DIR" => "目录创建失败" ,
  31. "IO" => "输入输出错误" ,
  32. "UNKNOWN" => "未知错误" ,
  33. "MOVE" => "文件保存时出错"
  34. );
  35. /**
  36. * 构造函数
  37. * @param string $fileField 表单名称
  38. * @param array $config 配置项
  39. * @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名
  40. */
  41. public function __construct( $fileField , $config , $base64 = false )
  42. {
  43. $this->fileField = $fileField;
  44. $this->config = $config;
  45. $this->stateInfo = $this->stateMap[ 0 ];
  46. $this->upFile( $base64 );
  47. }
  48. /**
  49. * 上传文件的主处理方法
  50. * @param $base64
  51. * @return mixed
  52. */
  53. private function upFile( $base64 )
  54. {
  55. //处理base64上传
  56. if ( "base64" == $base64 ) {
  57. $content = $_POST[ $this->fileField ];
  58. $this->base64ToImage( $content );
  59. return;
  60. }
  61. //处理普通上传
  62. $file = $this->file = $_FILES[ $this->fileField ];
  63. if ( !$file ) {
  64. $this->stateInfo = $this->getStateInfo( 'POST' );
  65. return;
  66. }
  67. if ( $this->file[ 'error' ] ) {
  68. $this->stateInfo = $this->getStateInfo( $file[ 'error' ] );
  69. return;
  70. }
  71. if ( !is_uploaded_file( $file[ 'tmp_name' ] ) ) {
  72. $this->stateInfo = $this->getStateInfo( "UNKNOWN" );
  73. return;
  74. }
  75. $this->oriName = $file[ 'name' ];
  76. $this->fileSize = $file[ 'size' ];
  77. $this->fileType = $this->getFileExt();
  78. if ( !$this->checkSize() ) {
  79. $this->stateInfo = $this->getStateInfo( "SIZE" );
  80. return;
  81. }
  82. if ( !$this->checkType() ) {
  83. $this->stateInfo = $this->getStateInfo( "TYPE" );
  84. return;
  85. }
  86. $this->fullName = $this->getFolder() . '/' . $this->getName();
  87. if ( $this->stateInfo == $this->stateMap[ 0 ] ) {
  88. if ( !move_uploaded_file( $file[ "tmp_name" ] , $this->fullName ) ) {
  89. $this->stateInfo = $this->getStateInfo( "MOVE" );
  90. }
  91. }
  92. }
  93. /**
  94. * 处理base64编码的图片上传
  95. * @param $base64Data
  96. * @return mixed
  97. */
  98. private function base64ToImage( $base64Data )
  99. {
  100. $img = base64_decode( $base64Data );
  101. $this->fileName = time() . rand( 1 , 10000 ) . ".png";
  102. $this->fullName = $this->getFolder() . '/' . $this->fileName;
  103. if ( !file_put_contents( $this->fullName , $img ) ) {
  104. $this->stateInfo = $this->getStateInfo( "IO" );
  105. return;
  106. }
  107. $this->oriName = "";
  108. $this->fileSize = strlen( $img );
  109. $this->fileType = ".png";
  110. }
  111. /**
  112. * 获取当前上传成功文件的各项信息
  113. * @return array
  114. */
  115. public function getFileInfo()
  116. {
  117. return array(
  118. "originalName" => $this->oriName ,
  119. "name" => $this->fileName ,
  120. "url" => $this->fullName ,
  121. "size" => $this->fileSize ,
  122. "type" => $this->fileType ,
  123. "state" => $this->stateInfo
  124. );
  125. }
  126. /**
  127. * 上传错误检查
  128. * @param $errCode
  129. * @return string
  130. */
  131. private function getStateInfo( $errCode )
  132. {
  133. return !$this->stateMap[ $errCode ] ? $this->stateMap[ "UNKNOWN" ] : $this->stateMap[ $errCode ];
  134. }
  135. /**
  136. * 重命名文件
  137. * @return string
  138. */
  139. private function getName()
  140. {
  141. return $this->fileName = time() . rand( 1 , 10000 ) . $this->getFileExt();
  142. }
  143. /**
  144. * 文件类型检测
  145. * @return bool
  146. */
  147. private function checkType()
  148. {
  149. return in_array( $this->getFileExt() , $this->config[ "allowFiles" ] );
  150. }
  151. /**
  152. * 文件大小检测
  153. * @return bool
  154. */
  155. private function checkSize()
  156. {
  157. return $this->fileSize <= ( $this->config[ "maxSize" ] * 1024 );
  158. }
  159. /**
  160. * 获取文件扩展名
  161. * @return string
  162. */
  163. private function getFileExt()
  164. {
  165. return strtolower( strrchr( $this->file[ "name" ] , '.' ) );
  166. }
  167. /**
  168. * 按照日期自动创建存储文件夹
  169. * @return string
  170. */
  171. private function getFolder()
  172. {
  173. $pathStr = $this->config[ "savePath" ];
  174. if ( strrchr( $pathStr , "/" ) != "/" ) {
  175. $pathStr .= "/";
  176. }
  177. $pathStr .= date( "Ymd" );
  178. if ( !file_exists( $pathStr ) ) {
  179. if ( !mkdir( $pathStr , 0777 , true ) ) {
  180. return false;
  181. }
  182. }
  183. return $pathStr;
  184. }
  185. }