簡體   English   中英

count($ _ FILES ['file'] ['tmp_name'])計算輸入,而不是上傳文件

[英]count($_FILES['file']['tmp_name']) counts the inputs instead of uploaded files

我有很多文件輸入,我想檢查一下一次上傳了多少文件:

<form id="propForm" class="option" name="imform" action="<?php echo $action[$option]; ?>" method="POST" enctype="multipart/form-data">
    <input type="file" name="file[]" accept="image/jpeg" />
    <input value="<?php echo $op[$option]; ?>" type="submit" name="submitIT">
</form>

在php文件中,我使用count()檢查:

$file_count = count($_FILES['file']['tmp_name']);

if ( $file_count > 0 && isset($_POST['submitIT']) ) {
    echo $file_count;
} else header('Location: /');

如果我沒有上傳文件就提交了,則回顯會顯示6(輸入的數量)。

這怎么可能?

編輯: $_FILE內容:

Array (
    [file] => Array (
        [name] => Array (
            [0] => 
        )
        [type] => Array (
            [0] => 
        )
        [tmp_name] => Array (
            [0] => 
        )
        [error] => Array (
            [0] => 4
            [1] => 4
            [2] => 4
            [3] => 4
            [4] => 4
            [5] => 4
        )
        [size] => Array (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
            [4] => 0
            [5] => 0
        )
    )
)

是的,當輸入的名稱與方括號name="file[]"同時,$ _ FILES就是這樣。 例如,具有3個輸入並且僅選擇了一個文件:

Array
(
    [file] => Array
    (
        [name] => Array
            (
                [0] => 
                [1] => leon-2.jpg
                [2] => 
            )

        [type] => Array
            (
                [0] => 
                [1] => image/jpeg
                [2] => 
            )

        [tmp_name] => Array
            (
                [0] => 
                [1] => C:\xampp\tmp\php447.tmp
                [2] => 
            )

        [error] => Array
            (
                [0] => 4
                [1] => 0
                [2] => 4
            )

        [size] => Array
            (
                [0] => 0
                [1] => 81945
                [2] => 0
            )

        )

    )

您可以檢查上傳了多少文件,如下所示:

$count = 0;
foreach ($_FILES['file']['tmp_name'] as $tmp_name) {
    if (is_uploaded_file($tmp_name)) {
        $count++;
    }
}

if ($count > 0) { 
  // do something... 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM