how to count number of uploaded files in php

十年热恋 提交于 2020-03-22 07:55:43

问题


How can i count the number of uploaded files? This is my form:

<div id="dragAndDropFiles" class="uploadArea">
        <h1>Drop Images Here</h1>
    </div>
    <form id="sfmFiler" class="sfmform" method="post" enctype="multipart/form-data">
        <input type="file" name="file" id="file" multiple />
        <input type="submit" name="submitHandler" id="submitHandler" class="buttonUpload" value="Upload">
    </form>

and this is the piece of php which uploads the files:

if($_SERVER['REQUEST_METHOD'] == "POST") {
    $tmpFilePath = $_FILES['file']['tmp_name'];
    $newFilePath = $dir.'/' . $_FILES['file']['name'];
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {
      echo "xxx files are successfully uploaded";
    }
} 

回答1:


In this code you are getting only one file thats why you are getting count result 1. if change your input file name like "file[]"

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

and then use the below line code you will get your desire result. Cause its needs an array filed to hold the input data.

 <?php echo count($_FILES['file']['name']); ?>

Thanks, i tried in my system get the result.




回答2:


AFriend is correct. The above answers always return 1.

Try:

echo count(array_filter($_FILES['file']['name']))

Worked for me anyway.

_t




回答3:


Check this answer

<?php echo count($_FILES['file']['name']); ?>

php multiple file uploads get the exact count of files a user uploaded and not the count of all input fields in the array




回答4:


Using the array_filter function it works

try

$countfiles = count(array_filter($_FILES['file']['name']));

It returns 0 in case of NULL, 1 in case of 1 file uploaded, etc.



来源:https://stackoverflow.com/questions/37363231/how-to-count-number-of-uploaded-files-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!