/hdphp2.0/hdphp/upload/Upload.php

https://github.com/houdunwang/video · PHP · 230 lines · 158 code · 31 blank · 41 comment · 25 complexity · 3ec55bb8cada5caa81c61013c5b6dea9 MD5 · raw file

  1. <?php
  2. /** .-------------------------------------------------------------------
  3. * | Software: [HDCMS framework]
  4. * | Site: www.hdcms.com
  5. * |-------------------------------------------------------------------
  6. * | Author: 向军 <2300071698@qq.com>
  7. * | WeChat: aihoudun
  8. * | Copyright (c) 2012-2019, www.houdunwang.com. All Rights Reserved.
  9. * '-------------------------------------------------------------------*/
  10. namespace hdphp\upload;
  11. class Upload {
  12. //上传类型
  13. protected $type;
  14. //上传文件大小
  15. protected $size;
  16. //上传路径
  17. protected $path;
  18. //错误信息
  19. protected $error;
  20. public function __construct() {
  21. //上传路径
  22. $this->path = Config::get( 'upload.path' );
  23. //上传类型
  24. $this->type = Config::get( 'upload.type' );
  25. //允许大小
  26. $this->size = Config::get( 'upload.size' ) * 1024;
  27. }
  28. /**
  29. * 上传
  30. *
  31. * @param null $fieldName 字段名
  32. *
  33. * @return array|bool
  34. * @throws Exception
  35. */
  36. public function make( $fieldName = NULL ) {
  37. if ( ! $this->createDir() ) {
  38. return FALSE;
  39. }
  40. $files = $this->format( $fieldName );
  41. $uploadedFile = [ ];
  42. //验证文件
  43. if ( ! empty( $files ) ) {
  44. foreach ( $files as $v ) {
  45. $info = pathinfo( $v ['name'] );
  46. $v["ext"] = isset( $info ["extension"] ) ? $info['extension'] : '';
  47. $v['filename'] = isset( $info['filename'] ) ? $info['filename'] : '';
  48. if ( ! $this->checkFile( $v ) ) {
  49. continue;
  50. }
  51. $upFile = $this->save( $v );
  52. if ( $upFile ) {
  53. $uploadedFile[] = $upFile;
  54. }
  55. }
  56. }
  57. return $uploadedFile;
  58. }
  59. //设置上传类型
  60. public function type( $type ) {
  61. $this->type = $type;
  62. return $this;
  63. }
  64. //设置上传大小
  65. public function size( $size ) {
  66. $this->size = $size * 1024;
  67. return $this;
  68. }
  69. //设置上传目录
  70. public function path( $path ) {
  71. $this->path = $path;
  72. return $this;
  73. }
  74. /**
  75. * 储存文件
  76. *
  77. * @param string $file 储存的文件
  78. *
  79. * @return boolean
  80. */
  81. private function save( $file ) {
  82. $fileName = mt_rand( 1, 9999 ) . time() . "." . $file['ext'];
  83. $filePath = $this->path . '/' . $fileName;
  84. if ( ! move_uploaded_file( $file ['tmp_name'], $filePath ) && is_file( $filePath ) ) {
  85. $this->error( '移动临时文件失败' );
  86. return FALSE;
  87. }
  88. $_info = pathinfo( $filePath );
  89. $arr = [ ];
  90. $arr['path'] = $filePath;
  91. $arr['url'] = __ROOT__ . '/' . $filePath;
  92. $arr['uptime'] = time();
  93. $arr['fieldname'] = $file['fieldname'];
  94. $arr['basename'] = $_info['basename'];
  95. $arr['filename'] = $_info['filename']; //新文件名
  96. $arr['name'] = $file['filename']; //旧文件名
  97. $arr['size'] = $file['size'];
  98. $arr['ext'] = $file['ext'];
  99. $arr['dir'] = $this->path;
  100. $arr['image'] = getimagesize( $filePath ) ? 1 : 0;
  101. return $arr;
  102. }
  103. //将上传文件整理为标准数组
  104. private function format( $fieldName ) {
  105. if ( $fieldName == NULL ) {
  106. $files = $_FILES;
  107. } else if ( isset( $_FILES[ $fieldName ] ) ) {
  108. $files[ $fieldName ] = $_FILES[ $fieldName ];
  109. }
  110. if ( ! isset( $files ) ) {
  111. $this->error = '没有任何文件上传';
  112. return FALSE;
  113. }
  114. $info = [ ];
  115. $n = 0;
  116. foreach ( $files as $name => $v ) {
  117. if ( is_array( $v ['name'] ) ) {
  118. $count = count( $v ['name'] );
  119. for ( $i = 0;$i < $count;$i ++ ) {
  120. foreach ( $v as $m => $k ) {
  121. $info [ $n ] [ $m ] = $k [ $i ];
  122. }
  123. $info [ $n ] ['fieldname'] = $name; //字段名
  124. $n ++;
  125. }
  126. } else {
  127. $info [ $n ] = $v;
  128. $info [ $n ] ['fieldname'] = $name; //字段名
  129. $n ++;
  130. }
  131. }
  132. return $info;
  133. }
  134. //创建目录
  135. private function createDir() {
  136. if ( ! is_dir( $this->path ) && ! mkdir( $this->path, 0755, TRUE ) ) {
  137. throw new Exception( "上传目录创建失败" );
  138. }
  139. return TRUE;
  140. }
  141. private function checkFile( $file ) {
  142. if ( $file ['error'] != 0 ) {
  143. $this->error( $file ['error'] );
  144. return FALSE;
  145. }
  146. if ( ! is_array( $this->type ) ) {
  147. $this->type = explode( ',', $this->type );
  148. }
  149. if ( ! in_array( strtolower( $file['ext'] ), $this->type ) ) {
  150. $this->error = '文件类型不允许';
  151. return FALSE;
  152. }
  153. if ( strstr( strtolower( $file['type'] ), "image" ) && ! getimagesize( $file['tmp_name'] ) ) {
  154. $this->error = '上传内容不是一个合法图片';
  155. return FALSE;
  156. }
  157. if ( $file ['size'] > $this->size ) {
  158. $this->error = '上传文件大于' . get_size( $this->size );
  159. return FALSE;
  160. }
  161. if ( ! is_uploaded_file( $file ['tmp_name'] ) ) {
  162. $this->error = '非法文件';
  163. return FALSE;
  164. }
  165. return TRUE;
  166. }
  167. private function error( $error ) {
  168. switch ( $error ) {
  169. case UPLOAD_ERR_INI_SIZE :
  170. $this->error = '上传文件超过PHP.INI配置文件允许的大小';
  171. break;
  172. case UPLOAD_ERR_FORM_SIZE :
  173. $this->error = '文件超过表单限制大小';
  174. break;
  175. case UPLOAD_ERR_PARTIAL :
  176. $this->error = '文件只上有部分上传';
  177. break;
  178. case UPLOAD_ERR_NO_FILE :
  179. $this->error = '没有上传文件';
  180. break;
  181. case UPLOAD_ERR_NO_TMP_DIR :
  182. $this->error = '没有上传临时文件夹';
  183. break;
  184. case UPLOAD_ERR_CANT_WRITE :
  185. $this->error = '写入临时文件夹出错';
  186. break;
  187. default:
  188. $this->error = '未知错误';
  189. }
  190. }
  191. /**
  192. * 返回上传时发生的错误原因
  193. *
  194. * @return string
  195. */
  196. public function getError() {
  197. return $this->error;
  198. }
  199. }