简体   繁体   中英

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:

<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 :

  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

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

While fd is not empty, $filename is 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'];  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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