Bra*_*cer 96 php directory file count
我正在开发一个稍微新的项目.我想知道如何制作它,以便计算某个目录中有多少文件.
<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>
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止(从搜索).但它出现不正常?我添加了一些注释,所以随意删除它们,它们只是让我尽可能地理解它.
如果您需要更多信息或觉得我没有详细描述,请随时说明.
Bab*_*aba 250
您可以简单地执行以下操作:
$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
Run Code Online (Sandbox Code Playgroud)
JKi*_*rtz 64
您可以像这样获取文件计数:
$directory = "/path/to/dir/";
$filecount = 0;
$files = glob($directory . "*");
if ($files){
$filecount = count($files);
}
echo "There were $filecount files";
Run Code Online (Sandbox Code Playgroud)
其中"*"是可以更改的特定文件类型,如果你想要一个像"*.jpg",或者你可以做多种文件类型是这样的:
glob($directory . "*.{jpg,png,gif}",GLOB_BRACE)
Run Code Online (Sandbox Code Playgroud)
该GLOB_BRACE标志展开{a,b,c}以匹配'a','b'或'c'
Lau*_*ieu 43
你应该有 :
<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>
Run Code Online (Sandbox Code Playgroud)
int*_*lis 41
试试这个.
// Directory
$directory = "/dir";
// Returns array of files
$files = scandir($directory);
// Count number of files and store them to variable..
$num_files = count($files)-2;
Run Code Online (Sandbox Code Playgroud)
不计算'.' 和'..'.
vbw*_*bwx 13
既然我也需要这个,我很好奇哪个选择最快.
我发现-如果你想要的是一个文件计数-巴巴的解决方案是很多比别人快.我很惊讶.
自己试试吧:
<?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);
}
?>
Run Code Online (Sandbox Code Playgroud)
测试运行:(显然,glob()不计算点文件)
1: 99 (0.4815571308136 s)
2: 98 (0.96104407310486 s)
3: 99 (0.26513481140137 s)
Run Code Online (Sandbox Code Playgroud)
Nir*_*ara 12
工作演示
<?php
$directory = "../images/team/harry/"; // dir location
if (glob($directory . "*.*") != false)
{
$filecount = count(glob($directory . "*.*"));
echo $filecount;
}
else
{
echo 0;
}
?>
Run Code Online (Sandbox Code Playgroud)
小智 9
我认为最好的答案是:
$num = count(glob("/exact/path/to/files/" . "*"));
echo $num;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
152783 次 |
| 最近记录: |