2012-07-19 79 views
-4
<form action="entergallery.php" method="post" enctype="multipart/form-data"> 
<select id="photolib" name="photolib"> 
<?php 
$query = "SELECT * FROM `galleries`"; 
$res = mysqli_query($db, $query); 
while($rows = mysqli_fetch_row($res)){ 
    echo '<option value="'.$rows[3].'">'.$rows[1].'</option>'; 
} 
?> 
</select> 
<input type="file" name="uploads[]" multiple/> 
<input type="submit" id="upload" name="upload" value="Upload"/> 
</form> 

由於某種原因,此表單總是傳遞5的長度。輸入gallery.php只是計數{$ _ FILES ['uploads']);即使沒有輸入任何文件,也總是輸出5。我不知道如何解決這個問題。我已經在多個瀏覽器測試,這一點,在所有 由於同樣的問題提前輸入總是發送5個大小

回答

0

Instea這

count{$_FILES['uploads'])

使用

count($_FILES['uploads']['name'])

的d你會得到實際計數。

1

見$ _FILES數組結構

總是有5個元素。

$_FILES["file"]["name"] - the name of the uploaded file 
$_FILES["file"]["type"] - the type of the uploaded file 
$_FILES["file"]["size"] - the size in bytes of the uploaded file 
$_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server 
$_FILES["file"]["error"] - the error code resulting from the file upload 

如果你想檢查$ _FILES爲空,則需要檢查上傳錯誤,這樣

<?php 
if ($_FILES["file"]["error"] > 0) 
    { 
    echo "Error: " . $_FILES["file"]["error"] . "<br />"; 
    } 
?> 

在你的情況下,有多個文件上傳你具有結構:

array 
    'filesToUpload' => 
    array 
     'name' => 
     array 
      0 => string '2012-07-19_192449.jpg' (length=21) 
      1 => string '2012-07-19_192449.png' (length=21) 
     'type' => 
     array 
      0 => string 'image/jpeg' (length=10) 
      1 => string 'image/png' (length=9) 
     'tmp_name' => 
     array 
      0 => string '/tmp/phpBp3Pf7' (length=14) 
      1 => string '/tmp/php4A25Ly' (length=14) 
     'error' => 
     array 
      0 => int 0 
      1 => int 0 
     'size' => 
     array 
      0 => int 5263 
      1 => int 8681 

等,文件數可以在tmp_name數組中計算上傳數組,例如