upload.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. header('content-type:application/json');
  3. session_start();
  4. include('../config.php');
  5. include('../includes/function.php');
  6. if(!$UPLOAD_IMAGE){
  7. exit('{"code":-8, "msg":"上传失败!上传接口被关闭!", "path":""}');
  8. }
  9. $filename = $_FILES['file']['name'];
  10. if ($filename) {
  11. $postfix = ['.png', '.jpg', '.jpeg'];
  12. $file_postfix = strtolower(mb_substr($filename, mb_strrpos($filename, '.')));
  13. if (!in_array($file_postfix, $postfix)) {
  14. exit('{"code":-1, "msg":"上传失败!文件类型不符合要求!", "path":""}');
  15. }
  16. $image_type = ['image/png', 'image/jpg', 'image/jpeg'];
  17. if (!in_array($_FILES['file']['type'], $image_type)) {
  18. exit('{"code":-2, "msg":"上传失败!文件类型错误!", "path":""}');
  19. }
  20. if ($_FILES['file']['size'] > $MAX_UPLOAD_SIZE * 1024) {
  21. exit('{"code":-3, "msg":"上传失败!文件过大", "path":""}');
  22. }
  23. if (!getimagesize($_FILES['file']["tmp_name"])) {
  24. exit('{"code":-4, "msg":"上传失败!读取图像文件失败!", "path":""}');
  25. }
  26. $filename_new = md5($VERIFICATION_KEY . strval(time()));
  27. $result = move_uploaded_file(
  28. $_FILES["file"]["tmp_name"],
  29. $UPLOAD_PATH . $filename_new . $file_postfix
  30. );
  31. if ($result) {
  32. exit('{"code":1, "msg":"上传成功!", "path":"' . $UPLOAD_PATH . $filename_new . $file_postfix . '"}');
  33. } else {
  34. exit('{"code":-5, "msg":"上传失败!未知错误!", "path":""}');
  35. }
  36. } else {
  37. exit('{"code":-6, "msg":"上传失败!没有文件!", "path":""}');
  38. }