How can I know a number of uploaded files with PHP?

后端 未结 4 849
深忆病人
深忆病人 2020-12-21 23:50

I have a form with several

input type=\"file\"

tags. How can I know on the server side amount of files uploaded by the user. He can upload

相关标签:
4条回答
  • 2020-12-22 00:23

    Form:

    <form enctype="multipart/form-data" ...>
    <input type="file" name="image[]" multiple>
    

    Script:

    $c = sizeof($_FILES['image']['name']);
    
    0 讨论(0)
  • 2020-12-22 00:28

    If you are having input upload tags with name like file1, file2 then

    if($_FILES['file1']['size'] > 0)
        echo "User uploaded some file for the input named file1"
    

    Now for many files (looking at the output you are having), run a foreach loop like this:-

    $cnt=0;
    foreach($_FILES as $eachFile)
    {
         if($eachFile['size'] > 0)
            $cnt++;
    }
    echo $cnt." files uploaded";
    

    I am not sure why the similar answer in How can I know a number of uploaded files with PHP? got downvoted? For the '0' ?

    0 讨论(0)
  • 2020-12-22 00:41

    You can use the count or sizeof on $_FILES array that contains uploaded file info:

     echo count($_FILES);
    

    Update (Based on comments):

    You can do this:

    $counter = 0;
    foreach($_FILES as $value){
      if (strlen($value['name'])){
        $counter++;
      }
    }
    
    echo $counter; // get files count
    
    0 讨论(0)
  • 2020-12-22 00:47

    $_FILES is a global array of files which stores uploaded files.

    0 讨论(0)
提交回复
热议问题