简体   繁体   English

用于异步文件上传的PHP代码

[英]PHP code for asynchronous file upload

I cannot find what is the right PHP code as to send to the server an image file... Consider the following: 我找不到将图像文件发送到服务器的正确的PHP代码。请考虑以下事项:

<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data">
      <div id="dragarea"></div>
      <input type="file" id="fileselect"  name="fileselect[]" multiple="multiple"/>
</form>

and the JS code which is executed upon a click event on the file I have dropped inside the dragarea : 以及在我拖放到dragarea内的文件上单击事件后执行的JS代码:

  var fd = new FormData($('#upload')[0]);
  $.ajax({
        method:"POST",
        url:"upload.php",
        dataType:"json",
        data:{fd1:fd}, 
        contentType: false,      
        cache: false,             
        processData:false,  
        success: function(data) {
            alert(data);
        } 
   })  

and my upload.php 和我的upload.php

$filename = $_FILES['fd1']['name'];  
echo json_encode( $filename); 

While fd is not empty, $filename is null. 当fd不为空时,$ filename为null。 Why that? 为什么?

Thank you 谢谢

Try replacing this line : 尝试替换此行:

data:{fd1:fd}, 

With this : 有了这个 :

data:fd,

So your file would be accessible with : 因此,您的文件可以通过以下方式访问:

$filename = $_FILES['fileselect'][0]['name'];  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM