在此場景中,您嘗試使用 PHP 和 jQuery 上傳多個檔案。為此,請按照以下步驟操作。
<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>
以上是如何使用 jQuery 在 PHP 中實作多個檔案上傳?的詳細內容。更多資訊請關注PHP中文網其他相關文章!