upload.php 1.9 KB

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