-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 只是 count{$_FILES['uploads']); 即使您不输入任何文件,它也始终输出 5。我不知道如何解决这个问题。我已经在多个浏览器中测试过这个并且有同样的问题提前谢谢

4

2 回答 2

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

因此,您可以在 filesUpload 数组的 tmp_name 数组中计算文件数,例如

于 2012-07-19T14:00:30.947 回答
0

而不是这个

count{$_FILES['uploads'])

采用

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

你会得到实际的计数。

于 2012-07-19T15:06:29.417 回答