ajax利用php上傳圖片

<script type="text/javascript">
  window.onload = function(){
    document.getElementById('btn').onclick = function(){
      var frm = document.getElementById('frm');
      var fd = new FormData(frm);
      var request = new XmlHttpRequest();
      request.open('post', './demo.php');
      request.onreadystatechange = function(){
        if (request.readystate == 4 && request.status == 200) {
          if(request.responseText == '1'){
            alert('上傳成功!');
          }else{
            alert('上傳失敗!');
          }
        }
      }
      request.send(fd);
    }
  }
</script>
</head>
<body>
  <form id="frm">
    用戶名:<input type="text" name="username"><br>
    頭像:<input type="file" name="face"><br>
    <input type="button" value="上傳" id="btn">
  </form>
</body>javascript

 

PHP代碼:php

<?phpjava

    $path = './Uploads/'.$_FILES['face']['name'];post

    echo move_uploaded_file($_FILES['face']['tmp_name'], $path);orm

?>ip

相關文章
相關標籤/搜索