使用PHP和JQuery上传多个文件

我最近一直在尝试使用PHP,到目前为止一直很好,直到碰到一堵墙为止。这是我的一小段代码。它允许我上传单个文件,但是我想要的是能够上传多个文件。


这是PHP和HTML文件:


<html>

<head>

  <meta charset="utf-8" />

  <title>Ajax upload form</title>


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

<script type="text/javascript">


    function sendfile(){

        var fd = new FormData();  


        for (var i = 0, len = document.getElementById('myfile').files.length; i < len; i++) {

            fd.append("myfile", document.getElementById('myfile').files[i]);                

        }


        $.ajax({

          url: 'uploadfile.php',

          data: fd,

          processData: false,

          contentType: false,

          type: 'POST',      

          success: function(data){

            alert(data);

          }

        });         

    }

  </script>


</head>

<body>

    <form action="uploadfile.php" method="post" enctype="multipart/form-data" id="form-id">


    <p><input id="myfile" type="file" name="myfile" multiple=multiple/>

    <input type="button" name="upload" id="upload" value="Upload" onclick="sendfile()" id="upload-button-id"  /></p>

    </form>

</body>

</html>

和PHP文件:


<?php


    $target = "uploadfolder/"; 

    //for($i=0; $i <count($_FILES['myfile']['name']); $i++){

        if(move_uploaded_file($_FILES['myfile']['tmp_name'], $target.$_FILES['myfile']['name'])) { 

            echo 'Successfully copied'; 


        }else{       

            echo 'Sorry, could not copy';

        }   

    }// 


?>

任何帮助将不胜感激。


Cats萌萌
浏览 588回答 3
3回答

LEATH

Index.html<html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title>Load files</title>&nbsp; &nbsp; &nbsp; &nbsp; <script src="jquery.min.js"></script>&nbsp; &nbsp; &nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(document).ready(function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('#myfiles').on("change", function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var myfiles = document.getElementById("myfiles");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var files = myfiles.files;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var data = new FormData();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (i = 0; i < files.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.append('file' + i, files[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'load.php',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contentType: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: data,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processData: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cache: false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).done(function(msg) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#loadedfiles").append(msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <div id="upload">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="fileContainer">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input id="myfiles" type="file" name="myfiles[]" multiple="multiple" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div id="loadedfiles">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </body></html>load.php<?php&nbsp; &nbsp; $path="myfiles/";//server path&nbsp; &nbsp; foreach ($_FILES as $key) {&nbsp; &nbsp; &nbsp; &nbsp; if($key['error'] == UPLOAD_ERR_OK ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = $key['name'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $temp = $key['tmp_name'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $size= ($key['size'] / 1000)."Kb";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; move_uploaded_file($temp, $path . $name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h12><strong>File Name: $name</strong></h2><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h12><strong>Size: $size</strong></h2><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <hr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ";&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $key['error'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }?>
打开App,查看更多内容
随时随地看视频慕课网APP