how to count number of uploaded files in php


Solution 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.

Solution 2

AFriend is correct. The above answers always return 1.


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

Worked for me anyway.


Solution 3

Using the array_filter function it works


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

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


Related videos on Youtube

Jack Maessen
Author by

Jack Maessen

I am just an ordinary swiminstructor in the Netherlands, but my hobby is creating websites and writing simple backend-scripts in PHP for websites. I like to learn about PHP and jQuery and try to share my knowledge with other people

Updated on June 14, 2022


  • Jack Maessen
    Jack Maessen 3 months

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

    <div id="dragAndDropFiles" class="uploadArea">
            <h1>Drop Images Here</h1>
        <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">

    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";
    • Murad Hasan
      Murad Hasan over 6 years
      try this: count($_FILES['file']['name'])
    • Saty
      Saty over 6 years
      Need array in file name as name="file[]"
  • Murad Hasan
    Murad Hasan over 6 years
    if you answer than make it clear. the op needs complete support. carry on.
  • A Friend
    A Friend over 4 years
    I could be wrong on this but count will always be 1 or greater unless the the paramter is NULL, so even if no files were uploaded count() would still equal 1