php+ajax+h5 to Upload Pictures

  • 2021-08-12 02:12:38
  • OfStack

This article example for everyone to share php ajax picture upload specific code, 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">

   Select a file :<input type="file" id="file1" /><br />
  <input type="button" id="upload" value=" Upload " />
  <span id="imgWait"></span>
</form>
<script>
  $(function () {
    $("#upload").click(function () {
      $("#imgWait").html(" Upload in ");
      var formData = new FormData();
      formData.append("myfile", document.getElementById("file1").files[0]);
      $.ajax({
        url: "/Home/index/fileupsend",
        type: "POST",
        data: formData,
        /**
         * Must false Will automatically add the correct Content-Type
         */
        contentType: false,
        /**
         *  Must false Will avoid jQuery Right  formdata  Default processing of 
         * XMLHttpRequest Will be right  formdata  Carry out correct treatment 
         */
        processData: false,
        success: function (data) {
          if(data){
            alert(" Upload successfully! ");
          }
          $("#imgWait").html(" Upload succeeded ");

        },
        error: function () {
          alert(" Upload failed! ");
          $("#imgWait").hide();
        }
      });
    });
  });
</script>
</body>
</html>

php code


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

}

Related articles: