Examples of implementing image upload function using php, ajax and h5

黄舟
Release: 2023-03-16 20:08:01
Original
1586 people have browsed it

The example of this article shares with you the specific code for php to implement ajaxpictureupload for your reference. The specific content is as follows

html page code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script type="text/javascript" src="PUBLIC/home/js/jquery-1.11.0.js"></script>

</head>
<body>
<form class="form-horizontal" role="form" id="myForm"
   action="/index/fileupsend" method="post"
   enctype="multipart/form-data">

  选择文件:<input type="file" id="file1" /><br />
  <input type="button" id="upload" value="上传" />
  <span id="imgWait"></span>
</form>
<script>
  $(function () {
    $("#upload").click(function () {
      $("#imgWait").html("上传中");
      var formData = new FormData();
      formData.append("myfile", document.getElementById("file1").files[0]);
      $.ajax({
        url: "/Home/index/fileupsend",
        type: "POST",
        data: formData,
        /**
         *必须false才会自动加上正确的Content-Type
         */
        contentType: false,
        /**
         * 必须false才会避开jQuery对 formdata 的默认处理
         * XMLHttpRequest会对 formdata 进行正确的处理
         */
        processData: false,
        success: function (data) {
          if(data){
            alert("上传成功!");
          }
          $("#imgWait").html("上传成功");

        },
        error: function () {
          alert("上传失败!");
          $("#imgWait").hide();
        }
      });
    });
  });
</script>
</body>
</html>
Copy after login

php code

public function fileupsend(){
  $type_pic = $this->file_upload(&#39;1&#39;,array(&#39;jpg&#39;, &#39;gif&#39;, &#39;png&#39;, &#39;jpeg&#39;),&#39;filetest&#39;,&#39;myfile&#39;);
  echo $type_pic[&#39;img_path&#39;];

}
Copy after login

The above is the detailed content of Examples of implementing image upload function using php, ajax and h5. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template