我正在开发一个稍微新的项目。我想知道如何制作它,以便计算某个目录中有多少文件。
<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>
这是我到目前为止(从搜索)。但它出现不正常?我已经添加了一些注释,所以请随意删除它们,它们只是让我尽可能地理解它。
如果您需要更多信息或觉得我没有详细描述,请随时说明。
答案 0 :(得分:240)
您可以执行以下操作:
$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
答案 1 :(得分:59)
您可以像这样获取文件计数:
$directory = "/path/to/dir/";
$filecount = 0;
$files = glob($directory . "*");
if ($files){
$filecount = count($files);
}
echo "There were $filecount files";
"*"
,如果您想要"*.jpg"
,或者您可以执行多种类型的文件类型,则可以将其更改为特定的文件类型:
glob($directory . "*.{jpg,png,gif}",GLOB_BRACE)
GLOB_BRACE
标记展开{a,b,c}以匹配'a','b'或'c'
答案 2 :(得分:42)
你应该:
<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>
答案 3 :(得分:37)
试试这个。
// Directory
$directory = "/dir";
// Returns array of files
$files = scandir($directory);
// Count number of files and store them to variable..
$num_files = count($files)-2;
不计算'。'和'..'。
答案 4 :(得分:12)
工作演示
<?php
$directory = "../images/team/harry/"; // dir location
if (glob($directory . "*.*") != false)
{
$filecount = count(glob($directory . "*.*"));
echo $filecount;
}
else
{
echo 0;
}
?>
答案 5 :(得分:12)
由于我也需要这个,我很好奇哪个选择最快。
我发现 - 如果你想要的只是文件数量 - Baba的解决方案很多比其他解决方案更快。我很惊讶。
亲自试试:
<?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);
}
?>
测试运行:(显然,glob()
不计算点文件)
1: 99 (0.4815571308136 s)
2: 98 (0.96104407310486 s)
3: 99 (0.26513481140137 s)
答案 6 :(得分:4)
我用这个:
count(glob("yourdir/*",GLOB_BRACE))
答案 7 :(得分:4)
我认为最好的答案:
$num = count(glob("/exact/path/to/files/" . "*"));
echo $num;
答案 8 :(得分:1)
<?php echo(count(array_slice(scandir($directory),2))); ?>
array_slice
与substr
函数类似,只适用于数组。
例如,这会从数组中删除前两个数组键:
$key_zero_one = array_slice($someArray, 0, 2);
如果你省略第一个参数,就像在第一个例子中一样,数组将不包含前两个键/值对*('。'和'..')。
答案 9 :(得分:1)
对某人有用。在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 "::"');
答案 10 :(得分:0)
$it = new filesystemiterator(dirname("Enter directory here"));
printf("There were %d Files", iterator_count($it));
echo("<br/>");
foreach ($it as $fileinfo) {
echo $fileinfo->getFilename() . "<br/>\n";
}
这应该有效 在dirname中输入目录。让魔法发生。
答案 11 :(得分:0)
$files = glob('uploads/*');
$count = 0;
$totalCount = 0;
$subFileCount = 0;
foreach ($files as $file)
{
global $count, $totalCount;
if(is_dir($file))
{
$totalCount += getFileCount($file);
}
if(is_file($file))
{
$count++;
}
}
function getFileCount($dir)
{
global $subFileCount;
if(is_dir($dir))
{
$subfiles = glob($dir.'/*');
if(count($subfiles))
{
foreach ($subfiles as $file)
{
getFileCount($file);
}
}
}
if(is_file($dir))
{
$subFileCount++;
}
return $subFileCount;
}
$totalFilesCount = $count + $totalCount;
echo 'Total Files Count ' . $totalFilesCount;
答案 12 :(得分:0)
根据接受的答案,这是一种递归计数目录中所有文件的方法:
function processlogin() {
console.log('in processlogin');
if(http.readyState == 4) {
console.log(http.readyState);
}
}
function buttonClicked() {
document.querySelector('#signin').addEventListener('click', dologin, false);
}
function dologin () {
console.log('in dologin');
}
document.addEventListener("DOMContentLoaded", buttonClicked);
var http = new XMLHttpRequest();
var website = "https://www.example.com/signin.php";
var params = "email=" + document.getElementById('email').value + "&password=" + document.getElementById('password').value;
http.open("POST", website);
http.responseType = 'text';
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = processlogin;
http.send(params);
答案 13 :(得分:0)
这是一个相当快的PHP Linux函数。有点脏,但是可以完成工作!
$ dir-目录路径
$ type-f,d或false(默认情况下)
f-仅返回文件计数
d-仅返回文件夹计数
false-返回文件和文件夹总数
function folderfiles($dir, $type=false) {
$f = escapeshellarg($dir);
if($type == 'f') {
$io = popen ( '/usr/bin/find ' . $f . ' -type f | wc -l', 'r' );
} elseif($type == 'd') {
$io = popen ( '/usr/bin/find ' . $f . ' -type d | wc -l', 'r' );
} else {
$io = popen ( '/usr/bin/find ' . $f . ' | wc -l', 'r' );
}
$size = fgets ( $io, 4096);
pclose ( $io );
return $size;
}
您可以进行调整以满足您的需求。
请注意,这不适用于Windows。
答案 14 :(得分:-2)
simple code add for file .php then your folder which number of file to count its
$directory = "images/icons";
$files = scandir($directory);
for($i = 0 ; $i < count($files) ; $i++){
if($files[$i] !='.' && $files[$i] !='..')
{ echo $files[$i]; echo "<br>";
$file_new[] = $files[$i];
}
}
echo $num_files = count($file_new);
简单地添加它的完成....