Home > Article > Backend Development > How to Implement Multiple File Uploads in PHP using jQuery?
In this scenario, you're attempting to upload multiple files using PHP and jQuery. To accomplish this, follow the steps outlined below.
<code class="html"><html> <head> <title>Load files</title> <script src="jquery.min.js"></script> <script> $(document).ready(function() { $("#myfiles").on("change", function() { var myfiles = document.getElementById("myfiles"); var files = myfiles.files; var data = new FormData(); for (i = 0; i < files.length; i++) { data.append('file' + i, files[i]); } $.ajax({ url: 'load.php', type: 'POST', contentType: false, data: data, processData: false, cache: false }).done(function(msg) { $("#loadedfiles").append(msg); }); }); }); </script> </head> <body> <div id="upload"> <div class="fileContainer"> <input id="myfiles" type="file" name="myfiles[]" multiple="multiple" /> </div> </div> <div id="loadedfiles"> </div> </body> </html></code>
<code class="php">$path="myfiles/";//server path foreach ($_FILES as $key) { if($key['error'] == UPLOAD_ERR_OK ){ $name = $key['name']; $temp = $key['tmp_name']; $size= ($key['size'] / 1000)."Kb"; move_uploaded_file($temp, $path . $name); echo " <div> <h12><strong>File Name: $name</strong></h2><br /> <h12><strong>Size: $size</strong></h2><br /> <hr> </div> "; }else{ echo $key['error']; } }</code>
The above is the detailed content of How to Implement Multiple File Uploads in PHP using jQuery?. For more information, please follow other related articles on the PHP Chinese website!