php 计算PHP目录中有多少文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12801370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count how many files in directory PHP
提问by Bradly Spicer
I'm working on a slightly new project. I wanted to know how many files are in a certain directory.
我正在做一个稍微新的项目。我想知道某个目录中有多少文件。
<div id="header">
<?php
$dir = opendir('uploads/'); # This is the directory it will count from
$i = 0; # Integer starts at 0 before counting
# While false is not equal to the filedirectory
while (false !== ($file = readdir($dir))) {
if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
}
echo "There were $i files"; # Prints out how many were in the directory
?>
</div>
This is what I have so far (from searching). However, it is not appearing properly? I have added a few notes so feel free to remove them, they are just so I can understand it as best as I can.
这是我到目前为止(从搜索)。但是,它没有正确显示?我已经添加了一些注释,所以可以随意删除它们,它们只是为了让我尽可能地理解它。
If you require some more information or feel as if I haven't described this enough please feel free to state so.
如果您需要更多信息或觉得我没有充分描述这一点,请随时说明。
回答by Baba
You can simply do the following :
您可以简单地执行以下操作:
$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
回答by JKirchartz
You can get the filecount like so:
您可以像这样获取文件计数:
$directory = "/path/to/dir/";
$filecount = 0;
$files = glob($directory . "*");
if ($files){
$filecount = count($files);
}
echo "There were $filecount files";
where the "*"is you can change that to a specific filetype if you want like "*.jpg"or you could do multiple filetypes like this:
当"*"是,如果你想要一个像您可以更改为特定文件类型"*.jpg"或者你可以做多种文件类型是这样的:
glob($directory . "*.{jpg,png,gif}",GLOB_BRACE)
the GLOB_BRACEflag expands {a,b,c} to match 'a', 'b', or 'c'
该GLOB_BRACE标志膨胀{A,B,C},以匹配'A', 'B',或'c'的
回答by intelis
Try this.
尝试这个。
// Directory
$directory = "/dir";
// Returns array of files
$files = scandir($directory);
// Count number of files and store them to variable..
$num_files = count($files)-2;
Not counting the '.' and '..'.
不算'.' 和 '..'。
回答by Laurent Brieu
You should have :
你应该有 :
<div id="header">
<?php
// integer starts at 0 before counting
$i = 0;
$dir = 'uploads/';
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
// prints out how many were in the directory
echo "There were $i files";
?>
</div>
回答by vbwx
Since I needed this too, I was curious as to which alternative was the fastest.
由于我也需要这个,我很好奇哪个替代方案最快。
I found that -- if all you want is a file count -- Baba's solution is a lotfaster than the others. I was quite surprised.
我发现-如果你想要的是一个文件计数-巴巴的解决方案是很多比别人快。我很惊讶。
Try it out for yourself:
自己试试吧:
<?php
define('MYDIR', '...');
foreach (array(1, 2, 3) as $i)
{
$t = microtime(true);
$count = run($i);
echo "$i: $count (".(microtime(true) - $t)." s)\n";
}
function run ($n)
{
$func = "countFiles$n";
$x = 0;
for ($f = 0; $f < 5000; $f++)
$x = $func();
return $x;
}
function countFiles1 ()
{
$dir = opendir(MYDIR);
$c = 0;
while (($file = readdir($dir)) !== false)
if (!in_array($file, array('.', '..')))
$c++;
closedir($dir);
return $c;
}
function countFiles2 ()
{
chdir(MYDIR);
return count(glob("*"));
}
function countFiles3 () // Fastest method
{
$f = new FilesystemIterator(MYDIR, FilesystemIterator::SKIP_DOTS);
return iterator_count($f);
}
?>
Test run: (obviously, glob()doesn't count dot-files)
测试运行:(显然,glob()不计算点文件)
1: 99 (0.4815571308136 s)
2: 98 (0.96104407310486 s)
3: 99 (0.26513481140137 s)
回答by vbwx
The best answer in my opinion:
我认为最好的答案:
$num = count(glob("/exact/path/to/files/" . "*"));
echo $num;
- It doesnt counts . and ..
- Its a one liner
- Im proud of it
- 这不算数。和 ..
- 它的一个班轮
- 我为此感到自豪
回答by Nirav Ranpara
Working Demo
工作演示
<?php
$directory = "../images/team/harry/"; // dir location
if (glob($directory . "*.*") != false)
{
$filecount = count(glob($directory . "*.*"));
echo $filecount;
}
else
{
echo 0;
}
?>
回答by Philipp Werminghausen
I use this:
我用这个:
count(glob("yourdir/*",GLOB_BRACE))
回答by Spooky
<?php echo(count(array_slice(scandir($directory),2))); ?>
array_sliceworks similary like substrfunction, only it works with arrays.
array_slice与substr函数类似,只是它适用于数组。
For example, this would chop out first two array keys from array:
例如,这将从数组中删除前两个数组键:
$key_zero_one = array_slice($someArray, 0, 2);
And if You ommit the first parameter, like in first example, array will not contain first two key/value pairs *('.' and '..').
如果您省略第一个参数,就像在第一个示例中一样,数组将不包含前两个键/值对 *('.' 和 '..')。
回答by Michel
Maybe usefull to someone. On a Windows system, you can let Windows do the job by calling the dir-command. I use an absolute path, like E:/mydir/mysubdir.
也许对某人有用。在 Windows 系统上,您可以通过调用 dir-command 让 Windows 完成这项工作。我使用绝对路径,例如E:/mydir/mysubdir.
<?php
$mydir='E:/mydir/mysubdir';
$dir=str_replace('/','\',$mydir);
$total = exec('dir '.$dir.' /b/a-d | find /v /c "::"');

