image_upload.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. define ( 'IN_BAMBOO', true );
  3. // 取得根目录
  4. define ( 'ROOT_PATH', '../../../../' ); // back to your root path
  5. $arrType = array (
  6. 'image/jpg',
  7. 'image/gif',
  8. 'image/png',
  9. 'image/bmp',
  10. 'image/pjpeg',
  11. 'image/jpeg'
  12. );
  13. $max_size = 500 * 1024; // 最大文件限制(单位:byte)
  14. $upfile = ROOT_PATH.'image/uploads'; // 图片目录路径
  15. if (!isset($_FILES ['files'])){
  16. echo '{"result":"400","msg":"未能找到图片,请确认图片是否过大"}';
  17. exit ();
  18. }
  19. $file = $_FILES ['files'];
  20. if ($_SERVER ['REQUEST_METHOD'] == 'POST') { // 判断提交方式是否为POST
  21. if (! is_uploaded_file ( $file ['tmp_name'] )) { // 判断上传文件是否存在
  22. echo '{"result":"400","msg":"图片不存在"}';
  23. exit ();
  24. }
  25. if ($file ['size'] > $max_size) { // 判断文件大小是否大于500000字节
  26. echo '{"result":"400","msg":"上传图片太大,最大支持:'.($max_size/1024).'KB"}';
  27. exit ();
  28. }
  29. if (! in_array ( $file ['type'], $arrType )) { // 判断图片文件的格式
  30. echo '{"result":"400","msg":"上传图片格式不对"}';
  31. exit ();
  32. }
  33. if (! file_exists ( $upfile )) { // 判断存放文件目录是否存在
  34. mkdir ( $upfile, 0777, true );
  35. }
  36. $imageSize = getimagesize ( $file ['tmp_name'] );
  37. $img = $imageSize [0] . '*' . $imageSize [1];
  38. $fname = $file ['name'];
  39. $ftype = explode ( '.', $fname );
  40. $time = explode ( " ", microtime () );
  41. $time = $time [1] . ($time [0] * 1000);
  42. $time2 = explode ( ".", $time );
  43. $time = $time2 [0];
  44. $returnName=$time."." .end($ftype);
  45. $picName = $upfile . "/" . $returnName ;
  46. if (! move_uploaded_file ( $file ['tmp_name'], $picName )) {
  47. echo '{"result":"400","msg":"从:'.$file ['tmp_name'].'移动图片到:'.$picName.'出错"}';
  48. exit ();
  49. } else {
  50. echo '{"result":"200","imgurl":"image/uploads/' . $returnName . '"}';
  51. }
  52. }
  53. ?>