2013-06-27 68 views
0

我想將多個文件上傳到目錄。 我已經「寫入」的代碼適用於一個文件,但是當我嘗試上傳多個文件時,它不起作用。我試圖確定故障在哪裏,我相信它與計數有關。儘管當我嘗試回顯有多少文件正在計數時,無論有多少文件被選中,我都會得到'1'。我知道這隻適用於一個文件,因爲我返回的變量總是'1',因此只能用於一個文件。PHP count()無法正確計數

收集我使用HTML表單與POST方法 HTML文件:

<form id="upload" action="upload.php" method="POST" enctype="multipart/form-data"> 

<fieldset> 
<legend>HTML File Upload</legend> 

<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" /> 

<div> 
    <label for="fileselect">Files to upload:</label> 
    <input type="file" id="fileselect" name="fileselect[]" multiple="multiple" /> 
    <div id="filedrag">or drop files here</div> 
</div> 

<div id="submit"> 
    <button type="submit">Upload Files</button> 
</div> 

</fieldset> 

</form> 

的JavaScript我使用使拖拽功能,並從被來源: http://www.sitepoint.com/html5-file-drag-and-drop/

罪魁禍首 PHP:

if(isset($_FILES['fileselect']['tmp_name'])) { 
    // Number of uploaded files 
    $num_files = count($_FILES['fileselect']['tmp_name']); 
    echo $num_files; 
    /** loop through the array of files ***/ 
    for($i=0; $i <= $num_files;$i++) { 
     // check if there is a file in the array 
     if(!is_uploaded_file($_FILES['fileselect']['tmp_name'][$i])) { 
      $messages[] = 'No file uploaded'; 
     } 
     else { 
      $unique = substr(number_format(time() * rand(),0,'',''),0,10); 

      $newImg = "img".$unique; 

      $filename = basename($_FILES['file']['name'][$i]); 
      $extension = pathinfo($filename, PATHINFO_EXTENSION); 
      //$new  = md5($filename).'.'.$extension; 

      $the_file_type = $_FILES['fileselect']['type'][$i]; 
      $the_file_size = $_FILES['fileselect']['size'][$i]/1024; 
      $the_file_name = $_FILES['fileselect']['name'][$i]; 

      $ok=1; 
      //This is our size condition 
      if ($uploaded_size > 350000) { 
       $messages[] = "Your file is too large.<br>"; 
       $ok=0; 
      } 

      //This is our limit file type condition 
      if ($uploaded_type =="text/php") { 
       $messages[] = "No PHP files<br>"; 
       $ok=0; 
      } 

      //Here we check that $ok was not set to 0 by an error 
      if ($ok==0) { 
       $messages[] = "Sorry your file was not uploaded"; 
      } 

      //If everything is ok we try to upload it 
      else { 
       if(move_uploaded_file($_FILES['fileselect']['tmp_name'][$i], "uploads/{$newImg}")) { 
        echo "The file ". basename($_FILES['uploadedfile']['name'][$i]). " has been uploaded"; 
        echo "<br />"; 
        //echo $the_file_type; 
        //echo "<br />"; 
        //echo $unique; 
        //echo "<span>schmeckle!</span>"; 
        //echo "<br />"; 
        //echo $the_file_size; 
        //echo "<br />"; 
        //echo $the_file_name; 

        $insertSQL = "INSERT INTO interviews_media_images SET "; 
         $insertSQL .= "fileType='$the_file_type', "; 
         $insertSQL .= "fileRef='$newImg', "; 
         $insertSQL .= "fileSize='$the_file_size', "; 
         $insertSQL .= "fileName='$the_file_name' "; 

        echo $insertSQL; 

        //mysql_query($insertSQL); 

        //echo mysql_error(); 

       } else { 
        $messages[] = "Sorry, there was a problem uploading your file."; 
       } 
      } 
     } 
    } 
} 
+1

你甚至'print_r($ _ FILES)'看看它實際上包含什麼?我相信它是'$ _FILES ['fileselect'] [$ i] ['attribute']'not'$ _FILES ['fileselect'] ['attribute'] [$ i]' – Twisted1919

+0

上面的所有內容:「for($ i = 0; $ i <= $ num_files; $ i ++){... blah} else {「,是從棧上的另一個帖子引用的,這顯然是有效的。 鏈接:http://stackoverflow.com/questions/2233816/how-to-handle-multiple-file-upload-using-php 下面的一切工作。我的問題是「$ num_files = count($ _ FILES ['fileselect'] ['tmp_name']);」總是返回'1'。 –

+0

我的第一個猜測是PHP'count()'函數中沒有錯誤 – chrislondon

回答

0

我創建了一個文件,內容如下,運行寧PHP版本5.5.15並正確計算:

<?php 
if(isset($_FILES['fileselect']['tmp_name'])) 
{ 
    echo "<pre>"; print_r($_FILES); echo "</pre>"; 
    echo "Number of files uploaded: " . count($_FILES['fileselect']['name']); 
} 
?> 

<form id="upload" action="files_upload.php" method="POST" enctype="multipart/form-data"> 

<fieldset> 
<legend>HTML File Upload</legend> 

<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000" /> 

<div> 
    <label for="fileselect">Files to upload:</label> 
    <input type="file" id="fileselect" name="fileselect[]" multiple="multiple" /> 
    <div id="filedrag">or drop files here</div> 
</div> 

<div id="submit"> 
    <button type="submit">Upload Files</button> 
</div> 

</fieldset> 

</form> 
+2

你需要更好地解釋你的答案 – RossBille

0

你的代碼是在我的情況下運行,$ NUM_FILES計數後,返回真值。

一件事做刪除 '=' 從循環,這樣寫

for($i=0; $i<$num_files; $i++) 

如果使用$ I < = $ NUM_FILES它給你一個通知。

假設您選擇3個文件,並且您正在設置$ i < = $ num_files

所以for循環從0開始,當你選擇了3個文件,它延續了4次

你可以看到,繼續循環4次,但對

$_FILES['filename']['tmp_name'][3]. 

我希望這將有助於你沒有文件...

對不起,我英語。