简体   繁体   English

如何使用PHP计算目录中的所有文件? (例如124个文件)

[英]How can I count all files in a directory with PHP? (e.g. 124 Files)

I'm trying to figure out how I can count all of my XML files in one directory. 我试图弄清楚如何在一个目录中计算所有XML文件。 How can I do this with PHP? 我该如何使用PHP?

$dir = "random_directory/";
$files = glob("$dir*.xml");
$count = $files !== false ? count($files) : 0;
echo $count;

Just change $dir to your directory. 只需将$ dir更改为目录即可。

you can use glob 你可以使用glob

$count = 0;

foreach (glob("*.xml") as $filename) {
    echo $filename;  //gives you the file name
    $count++;
}

echo $count;

or per MitMaro, simply just get the # of xml files without accessing file information: 或每个MitMaro,只需获取xml文件数即可,而无需访问文件信息:

echo count(glob("*.xml"));
$files = scandir('/path/to/dir/');
$count = 0;
foreach ($files as $file) {
    if (strtolower(substr($file, 4)) == '.xml') {
        $count++;
    }
}

http://php.net/scandir http://php.net/scandir

http://php.net/substr http://php.net/substr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何自定义FancyUpload以上传多个文件,每个文件都有自己的数据(例如标题,标题)? - How can I customize FancyUpload to upload multiple files, each with its own data (e.g. caption, title)? 如何提取某些HTML标签,例如 <ul> 在PHP中使用带有preg_match_all的Regex? - How can I extract certain HTML tags e.g. <ul> using Regex with preg_match_all in PHP? 如何在PHP中自然地排序文件列表(一些以数字结尾,例如“-2”) - How to naturally sort a list of files (some ending with numbers, e.g. “-2”) in PHP 如何在PHP中使用Ajax从网站(例如IMDB)中获取数据 - How can I fetch data from a website (e.g. IMDB ) using Ajax in PHP 如何在特定时间间隔内运行PHP脚本(例如每天一次)? - How can I run PHP script in certain interval (e.g. once a day)? 如何在PHP中循环$ num,例如1、2、3、4、1、2、3、4、1、2、3、4 - How to loop $num in php, e.g. 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 在PHP中将$ this用作文件级变量(例如,跨越文件的函数)是否是一种好/中立/坏习惯? - Is it a good/neutral/bad practice to use $this as a file-level variable in PHP (e.g. functions spanning files)? 我如何在jQuery中的页面加载时播放动画,以便它播放index.php而不播放other_page.php - How can I play an animation on page load in jQuery so that it plays for index.php but not e.g. other_page.php 如何使用php将所有图像文件从目录移动到子目录 - How can i move all image files from a directory to a sub-directory using php 如何拒绝访问目录中的所有文件,但允许访问其他目录中的特定php文件呢? - How can I deny access to all files in directory but allow access from specific php file in other directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM