Uploader.class.php 6.0 KB

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