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

北城以北 提交于 2019-12-18 09:27:13

问题


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 3 files, or may be 5 files, 1 or even nothing. I need to know how much files user have uploaded.


回答1:


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' ?




回答2:


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



回答3:


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




回答4:


Form:

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

Script:

$c = sizeof($_FILES['image']['name']);


来源:https://stackoverflow.com/questions/4367861/how-can-i-know-a-number-of-uploaded-files-with-php

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