editormd.uploader.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. *
  4. * Editor.md PHP简易上传类(仅供演示用,生产环境使用还需谨慎考虑)
  5. *
  6. * @FileName: editormd.uploader.class.php
  7. * @Auther: Pandao
  8. * @E-mail: pandao@vip.qq.com
  9. * @CreateTime: 2015-02-13 23:31:32
  10. * @UpdateTime: 2015-06-02 15:36:23
  11. * Copyright@2015 Editor.md all right reserved.
  12. */
  13. class EditorMdUploader
  14. {
  15. public $files; // $_FILES数组
  16. public $fileExit; // 文件扩展名
  17. public $saveName; // 最终保存的文件名
  18. public $saveURL; // 最终保存URL地址
  19. public $savePath; // 保存本地文件路径
  20. public $randomLength = ''; // 生成随机文件名的长度,当为日期时为日期的格式
  21. public $randomNameType = 1; // 生成随机的形式, NULL为保留原文件名, 1生成随机字符串, 2生成日期文件名
  22. public $formats = array( // 允许上传的文件格式
  23. 'gif', 'jpg', 'jpeg', 'png', 'bmp'
  24. );
  25. public $maxSize = 1024; // 最大上传文件大小,单位KB
  26. public $cover = true; // 是否覆盖同名文件, 1覆盖,0不覆盖
  27. public $redirect = false; // 是否进行URL跳转
  28. public $redirectURL = ""; // 上传成功或出错后要转到的URL
  29. public $errors = array( // 错误信息
  30. 'empty' => '上传文件不能为空',
  31. 'format' =>'上传的文件格式不符合规定',
  32. 'maxsize' => '上传的文件太大',
  33. 'unwritable' => '保存目录不可写,请更改权限',
  34. 'not_exist' => '保存目录不存在',
  35. 'same_file' => '已经有相同的文件存在'
  36. );
  37. /**
  38. * 构造函数,初始化对象
  39. *
  40. * @access public
  41. * @param string $savePath 最终保存的本地路径
  42. * @param string $saveURL 最终保存的URL地址
  43. * @param string $formats 允许上传的文件格式
  44. * @param Number $randomNameType 是否生成随机文件名及形式
  45. * @param Intiger/string $randomLength 生成随机文件名字符的长度
  46. * @param boolean $cover 是否覆盖相同文件
  47. * @param Intiger $maxSize 允许最大的上传文件大小,以KB为单位
  48. * @return viod
  49. */
  50. public function __construct($savePath, $saveURL, $formats, $randomNameType = 1, $randomLength = '', $cover = true, $maxSize = 1024)
  51. {
  52. $this->savePath = $savePath;
  53. $this->saveURL = $saveURL;
  54. $this->formats = $formats;
  55. $this->maxSize = $maxSize;
  56. $this->cover = $cover;
  57. $this->randomNameType = $randomNameType;
  58. $this->randomLength = $randomLength;
  59. }
  60. /**
  61. * 配置参数函数
  62. *
  63. * @access public
  64. * @param array $configs 配置项数组
  65. * @return void
  66. */
  67. public function config($configs)
  68. {
  69. foreach($configs as $key => $value)
  70. {
  71. $this->$key = $value;
  72. }
  73. }
  74. /**
  75. * 执行文件上传
  76. *
  77. * @access public
  78. * @param string $name fileInput's name
  79. * @return boolean 返回是否上传成功的布尔值
  80. */
  81. public function upload($name)
  82. {
  83. if(empty($_FILES[$name]['name'])) //上传文件为空时
  84. {
  85. $this->message($this->errors['empty']);
  86. return false;
  87. }
  88. $this->files = $_FILES[$name];
  89. if(!file_exists($this->savePath)) //目录不存在
  90. {
  91. $this->message($this->errors['not_exist']);
  92. return false;
  93. }
  94. if(!is_writable($this->savePath)) //目录不可写
  95. {
  96. $this->message($this->errors['unwritable']);
  97. return false;
  98. }
  99. $this->fileExt = $this->getFileExt($this->files["name"]); //取得扩展名
  100. $this->setSeveName();
  101. return $this->moveFile();
  102. }
  103. /**
  104. * 检查并移动上传文件
  105. *
  106. * @access private
  107. * @return boolean
  108. */
  109. private function moveFile()
  110. {
  111. $files = $this->files;
  112. if ($this->formats != "" && !in_array($this->fileExt, $this->formats))
  113. {
  114. $formats = implode(',', $this->formats);
  115. $message = "您上传的文件" . $files["name"] . "是" . $this->fileExt . "格式的,系统不允许上传,您只能上传" . $formats . "格式的文件。";
  116. $this->message($message);
  117. return false;
  118. }
  119. if ($files["size"] / 1024 > $this->maxSize)
  120. {
  121. $message = "您上传的 " . $files["name"] . ",文件大小超出了系统限定值" . $this->maxSize . " KB,不能上传。";
  122. $this->message($message);
  123. return false;
  124. }
  125. if (!$this->cover) //当不能覆盖时
  126. {
  127. if(file_exists($this->savePath.$this->saveName)) //有相同的文件存在
  128. {
  129. $this->message($this->saveName . $this->errors['same_file']);
  130. return false;
  131. }
  132. }
  133. if (!@move_uploaded_file($files["tmp_name"], iconv("utf-8", "gbk", $this->savePath . $this->saveName)))
  134. {
  135. switch($files["errors"])
  136. {
  137. case '0':
  138. $message = "文件上传成功";
  139. break;
  140. case '1':
  141. $message = "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
  142. break;
  143. case '2':
  144. $message = "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值";
  145. break;
  146. case '3':
  147. $message = "文件只有部分被上传";
  148. break;
  149. case '4':
  150. $message = "没有文件被上传";
  151. break;
  152. case '6':
  153. $message = "找不到临时目录";
  154. break;
  155. case '7':
  156. $message = "写文件到硬盘时出错";
  157. break;
  158. case '8':
  159. $message = "某个扩展停止了文件的上传";
  160. break;
  161. case '999':
  162. default:
  163. $message = "未知错误,请检查文件是否损坏、是否超大等原因。";
  164. break;
  165. }
  166. $this->message($message);
  167. return false;
  168. }
  169. @unlink($files["tmp_name"]); //删除临时文件
  170. return true;
  171. }
  172. /**
  173. * 生成随机文件名函数
  174. *
  175. * @access private
  176. * @return string $fileName 返回生成的文件名字符串
  177. */
  178. private function randomFileName()
  179. {
  180. if ($this->randomNameType == 1) // 生成时间格式文件名
  181. {
  182. date_default_timezone_set('PRC'); //设置时区
  183. $date = date($this->randomLength);
  184. $fileName = $date . "_" . mt_rand(10000, 99999);
  185. }
  186. elseif ($this->randomNameType == 2) // 生成随机字符文件名
  187. {
  188. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  189. $max = strlen($chars) - 1;
  190. mt_srand((double)microtime() * 1000000);
  191. for($i = 0; $i < $this->randomLength; $i++)
  192. {
  193. $fileName .= $chars[mt_rand(0, $max)];
  194. }
  195. }
  196. else
  197. {
  198. $fileName = '';
  199. }
  200. return $fileName;
  201. }
  202. /**
  203. * 组成最终保存的完整路径及文件名
  204. *
  205. * @access private
  206. * @return void
  207. */
  208. private function setSeveName()
  209. {
  210. $this->saveName = $this->randomFileName();
  211. if($this->saveName == '') //如果没生成随机文件名,就保留原文件名
  212. {
  213. $this->saveName = $this->files['name'];
  214. }
  215. }
  216. /**
  217. * 取得保存的文件名,用于数据库存储
  218. *
  219. * @access public
  220. * @return string
  221. */
  222. public function getSeveName()
  223. {
  224. return $this->saveName;
  225. }
  226. /**
  227. * 获取文件后缀名函数
  228. *
  229. * @access public
  230. * @return string
  231. */
  232. public function getFileExt($fileName)
  233. {
  234. return trim(strtolower(substr(strrchr($fileName, '.'), 1)));
  235. }
  236. /**
  237. * 上传成功或出错后跳转
  238. *
  239. * @access public
  240. * @param array $configs 配置项数组
  241. * @return void
  242. */
  243. public function redirect()
  244. {
  245. header('location: ' . $this->redirectURL);
  246. }
  247. /**
  248. * 错误提示函数
  249. *
  250. * @access public
  251. * @return void
  252. */
  253. public function message($message, $success = 0)
  254. {
  255. $array = array(
  256. 'success' => $success
  257. );
  258. $url = $this->saveURL . $this->saveName;
  259. // 适用于跨域上传时,跳转到中介页面等
  260. if ($this->redirect)
  261. {
  262. $this->redirectURL .= "&success=" . $success . "&message=" . $message;
  263. if ($success == 1)
  264. {
  265. $this->redirectURL .= '&url=' . $url;
  266. }
  267. $this->redirect();
  268. }
  269. else
  270. {
  271. if ($success == 1)
  272. {
  273. $array['url'] = $url;
  274. }
  275. else
  276. {
  277. $array['message'] = $message;
  278. }
  279. echo json_encode($array);
  280. }
  281. }
  282. }