Blogger Information
Blog 19
fans 0
comment 1
visits 19500
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Jquery+AJAX上传文件,无刷新上传并重命名文件
TANKING的代码日志
Original
1099 people have browsed it

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Ajax上传图片</title>
  5. <meta charset="utf-8">
  6. <script src="https://www.jq22.com/jquery/jquery-3.3.1.js"></script>
  7. </head>
  8. <body>
  9. <h1>Ajax上传图片</h1>
  10. <!-- 表单 -->
  11. <form id="form" enctype="multipart/form-data">
  12. <input type="file" id="fileAttach" name="file"/>
  13. <input type="button" onclick="upload()" value="上传"/>
  14. </form>
  15. <!-- 显示结果 -->
  16. <h2 id="upload-result"></h2>
  17. <!-- 显示图片 -->
  18. <div id="imgdiv"></div>
  19. <!-- Ajax上传 -->
  20. <script>
  21. function upload(){
  22. var form = new FormData(document.getElementById("form"));
  23. $.ajax({
  24. url:"upload.php",
  25. type:"post",
  26. data:form,
  27. cache: false,
  28. processData: false,
  29. contentType: false,
  30. success:function(data){
  31. if (data.res == "400") {
  32. $("#upload-result").text("上传成功");
  33. $("#imgdiv").html("<img src=\"upload/"+data.path+"\"/>");
  34. }else if (data.res == "403") {
  35. $("#upload-result").text("格式不对");
  36. }else if (data.res == "404") {
  37. $("#upload-result").text("上传错误");
  38. }
  39. },
  40. error:function(data){
  41. alert("上传失败")
  42. }
  43. })
  44. }
  45. </script>
  46. </body>
  47. </html>

upload.php

  1. <?php
  2. header("Content-type:application/json");
  3. //获取原始文件名
  4. $filename = $_FILES["file"]["name"];
  5. //获取文件后缀名
  6. $hzm = substr($filename,strpos($filename,"."));
  7. //设置新文件名
  8. $newfilename = substr(str_shuffle("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"),26,10);
  9. // 允许上传的图片后缀
  10. $allowedExts = array("gif", "jpeg", "jpg", "png");
  11. $temp = explode(".", $filename);
  12. $extension = end($temp);
  13. if ((($_FILES["file"]["type"] == "image/gif")
  14. || ($_FILES["file"]["type"] == "image/jpeg")
  15. || ($_FILES["file"]["type"] == "image/jpg")
  16. || ($_FILES["file"]["type"] == "image/pjpeg")
  17. || ($_FILES["file"]["type"] == "image/x-png")
  18. || ($_FILES["file"]["type"] == "image/png"))
  19. && ($_FILES["file"]["size"] < 2048000) // 小于 2000 kb
  20. && in_array($extension, $allowedExts))
  21. {
  22. if ($_FILES["file"]["error"] > 0)
  23. {
  24. echo "{\"res\":\"404\"}";
  25. }
  26. else
  27. {
  28. // 此处可以输出文件的详细信息
  29. if (file_exists("upload/" . $newfilename.$hzm))
  30. {
  31. //
  32. }
  33. else
  34. {
  35. move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename.$hzm);
  36. echo "{\"path\":\"$newfilename$hzm\",\"res\":\"400\"}";
  37. }
  38. }
  39. }
  40. else
  41. {
  42. echo "{\"res\":\"403\"}";
  43. }
  44. ?>

请在当前目录建立upload文件夹用于存放上传后的图片

GiF Demo

Author

Name:TANKING
Date:2020-04-11
Web:LIKEYUNBA.COM
WeChat:face6009

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
相逢恨晚 2020-04-16 13:39:10
可以的,可以的
1 floor