upload.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. //echo $name_tmp;
  32. //echo $pic_url;
  33. //$a=move_uploaded_file($name_tmp, $pic_url);
  34. //echo $a;
  35. //exit;
  36. //上传后图片路径+名称
  37. if (move_uploaded_file($name_tmp, $pic_url)) {//临时文件转移到目标文件夹
  38. $url = "/uploads/". $pic_name;
  39. echo json_encode(array("error" => "0", "pic" => $url, "name" => $pic_name));
  40. } else {
  41. echo json_encode(array("error" => "上传有误,清检查服务器配置!"));
  42. }
  43. }