2013-10-01 97 views
0

我已經嘗試過以下代碼用於大尺寸視頻上傳。它一次只上傳一個文件,但我想一次上傳多個文件,我應該怎麼做?上傳服務器上的mulltiple大尺寸視頻

<?php 
$target_path = "C:/xampp/htdocs/uploadVideo/uploadedFile/"; 
$tmp_name = $_FILES['fileToUpload']['tmp_name']; 
$size = $_FILES['fileToUpload']['size']; 
$name = $_FILES['fileToUpload']['name']; 
$name2 = $_GET['filename']; 
$target_file = $target_path.$name; 
$complete =$target_path.$name2; 
$com = fopen($complete, "ab"); 
error_log($target_path); 
    $in = fopen($tmp_name, "rb"); 
     if ($in) { 
      while ($buff = fread($in, 1048576)) { 
       fwrite($com, $buff); 
      } 
     } 
     fclose($in); 
    fclose($com); 
?> 

和JavaScript代碼是在這裏:

<script type="text/javascript" > 
function uploadchange() { 
      var input = document.getElementById("file"); 
      var ul = document.getElementById("uploadlist"); 
      while (ul.hasChildNodes()) { 
       ul.removeChild(ul.firstChild); 
      } 
      for (var i = 0; i < input.files.length; i++) { 
       var li = document.createElement("li"); 
       thefilesize = input.files[i].fileSize||input.files[i].size; 
       if (thefilesize > 1024 * 1024){ 
          thefilesize = (Math.round(thefilesize * 100/(1024 * 1024))/100).toString() + 'MB'; 
         }else{ 
           thefilesize = (Math.round(thefilesize * 100/1024)/100).toString() + 'KB'; 
       } 

       li.innerHTML = input.files[i].name + " " + thefilesize ; 
       ul.appendChild(li);    
      } 
      if(!ul.hasChildNodes()) { 
       var li = document.createElement("li"); 
       li.innerHTML = 'No Files Selected'; 
       ul.appendChild(li); 
      } 
      sendRequest(); 

     } 

window.BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder; 

      function sendRequest() { 
     var blob = document.getElementById('file').files[0]; 
       var BYTES_PER_CHUNK = 1048576; // 1MB chunk sizes. 
       var SIZE = blob.size; 
       var start = 0; 
       var end = BYTES_PER_CHUNK; 
     window.uploadcounter=0; 
     window.uploadfilearray = []; 
     document.getElementById('progressNumber').innerHTML = "Upload: 0 % "; 
       while(start < SIZE) { 

        var chunk = blob.slice(start, end); 
      window.uploadfilearray[window.uploadcounter]=chunk; 
         window.uploadcounter=window.uploadcounter+1; 
        start = end; 
        end = start + BYTES_PER_CHUNK; 
       } 
     window.uploadcounter=0; 
     uploadFile(window.uploadfilearray[window.uploadcounter],document.getElementById('file').files[0].name); 
      } 

      function fileSelected() { 
       var file = document.getElementById('fileToUpload').files[0]; 
       if (file) { 
        var fileSize = 0; 
        if (file.size > 1024 * 1024) 
         fileSize = (Math.round(file.size * 100/(1024 * 1024))/100).toString() + 'MB'; 
        else 
         fileSize = (Math.round(file.size * 100/1024)/100).toString() + 'KB'; 

        document.getElementById('fileName').innerHTML = 'Name: ' + file.name; 
        document.getElementById('fileSize').innerHTML = 'Size: ' + fileSize; 
        document.getElementById('fileType').innerHTML = 'Type: ' + file.type; 
       } 
      } 

      function uploadFile(blobFile,filename) { 
       var fd = new FormData(); 
       fd.append("fileToUpload", blobFile); 

       var xhr = new XMLHttpRequest(); 


       xhr.addEventListener("load", uploadComplete, false); 
       xhr.addEventListener("error", uploadFailed, false); 
       xhr.addEventListener("abort", uploadCanceled, false); 

       xhr.open("POST", "c.php?filename="+filename); 

       xhr.onload = function(e) { 
      window.uploadcounter=window.uploadcounter+1; 
      if (window.uploadfilearray.length > window.uploadcounter){ 
       uploadFile(window.uploadfilearray[window.uploadcounter],document.getElementById('file').files[0].name); 
       var percentloaded2 = parseInt((window.uploadcounter/window.uploadfilearray.length)*100); 
       document.getElementById('progressNumber').innerHTML = 'Upload: '+percentloaded2+' % ';        
      }else{ 
       document.getElementById('progressNumber').innerHTML = "File uploaded"; 
       loadXMLDoc('c.php?url='+ window.currentuploaddir); 

      } 
        }; 

       xhr.send(fd); 

      } 

      function uploadComplete(evt) { 
       /* This event is raised when the server send back a response */ 
     if (evt.target.responseText != ""){ 
        alert(evt.target.responseText); 
     } 
      } 

      function uploadFailed(evt) { 
       alert("There was an error attempting to upload the file."); 
      } 

      function uploadCanceled(evt) { 
       xhr.abort(); 
       xhr = null; 
       //alert("The upload has been canceled by the user or the browser dropped the connection."); 
      } 


</script> 



</head> 
<body> 

    <h3>Upload file here</h3> 
    <br /> 
    <form name="form1" method="post" action="" enctype="multipart/form-data"> 

    <div id="progressNumber"></div> 
    <input type="file" id="file"> 
    <div id="uploadlist"></div> 

    <input type="button" value="Upload" onClick="uploadchange();"> 



</form> 

我應該在一個時間多文件上傳辦? 我想上傳大尺寸文件和多個文件...此代碼一次只能處理一個文件..如何使用此代碼上傳多個文件。如何在此代碼中申請循環以進行多次上傳?

+0

我的建議嘗試[Jquery File Upload Plugin](http://blueimp.github.io/jQuery-File-Upload)如果你想多次上傳。 –

回答

0

開始在你的HTML改變

<input type="file" id="file"> 

<input type="file" name="file[]" id="file" multiple /> 

然後在您的php中爲用戶上傳的每個文件創建一個循環。

這是我最近一次上傳多個文件;

if($_SERVER['REQUEST_METHOD'] == "POST") { 

foreach ($_FILES['file']['name'] as $f => $name) { 

    $allowedExts = array("pdf", "jpeg", "jpg", "png"); 
    $temp = explode(".", $name); 
    $extension = end($temp); 
    $naam = rand(1111111,9999999); 
    if ($_FILES["file"]["size"][$f] < 200000000000 && in_array($extension, $allowedExts)) 
    { 
     if ($_FILES["file"]["error"][$f] > 0) 
     { 
      echo "Error: " . $_FILES["file"]["error"][$f] . "<br>"; 
     } 
     else 
     { 
      $filenaam = $naam . "." .$extension; 
      move_uploaded_file($_FILES["file"]["tmp_name"][$f], "/your/path/goes/here/" . $filenaam); 
      echo $filenaam . "<br />"; 
     } 
    } 
    else 
    { 
     echo "Wrong file."; 
    } 
} 
} 
+0

move_uploaded_file函數將只適用於小尺寸的文件...如何申請循環在我的JavaScript代碼? – user2809576

+0

然後像以前的php腳本一樣使用'fwrite'。 –