upload.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. $typeArr = array("jpg", "png", "gif", "ico");
  3. //允许上传文件格式
  4. $path = "../uploads/";
  5. //上传路径
  6. //if (!file_exists($path)) {
  7. // mkdir($path);
  8. //}
  9. if (isset($_POST)) {
  10. $name = $_FILES['file']['name'];
  11. $size = $_FILES['file']['size'];
  12. $name_tmp = $_FILES['file']['tmp_name'];
  13. // echo $name_tmp;
  14. if (empty($name)) {
  15. echo json_encode(array("error" => "您还未选择图片"));
  16. exit;
  17. }
  18. $type = strtolower(substr(strrchr($name, '.'), 1));
  19. //获取文件类型
  20. if (!in_array($type, $typeArr)) {
  21. echo json_encode(array("error" => "清上传jpg,png或gif类型的图片!"));
  22. exit;
  23. }
  24. // if ($size > (500 * 1024)) {
  25. // echo json_encode(array("error" => "图片大小已超过500KB!"));
  26. // exit ;
  27. // }
  28. $pic_name = time() . rand(10000, 99999) . "." . $type;
  29. //图片名称
  30. $pic_url = $path . $pic_name;
  31. //上传后图片路径+名称
  32. if (move_uploaded_file($name_tmp, $pic_url)) {//临时文件转移到目标文件夹
  33. $url = "/uploads/". $pic_name;
  34. echo json_encode(array("error" => "0", "pic" => $url, "name" => $pic_name));
  35. } else {
  36. echo json_encode(array("error" => "上传有误,清检查服务器配置!"));
  37. }
  38. }