php - 如何统计一个目录下的文件个数?

标签 php laravel-5.2

我想知道如何使用 Laravel 5.2 计算目录中的文件数:

$folderPath="upload/";
$countFile=0;
$totalFiles=glob($folderPath."*");
if($totalFiles){
    $countFile=count($totalFiles);
}
print_r($countFile);

这对我不起作用。

最佳答案

根据 5.6 documentation,您可以使用 filesallFiles 计算目录中的文件数(不过,此功能自 5.0 以来一直存在,而您的问题专门针对 5.2)。

filesallFiles 的区别在于allFilesfiles 不同,它会递归搜索子目录。同样,根据文档:

The files method returns an array of all of the files in a given directory. If you would like to retrieve a list of all files within a given directory including all sub-directories, you may use the allFiles method:

以及提供的示例代码:

use Illuminate\Support\Facades\Storage;

$files = Storage::files($directory);

$files = Storage::allFiles($directory);

注意:这个答案是作为旧答案的更详尽版本提供的(它为我指明了正确的方向,但我个人想确认它仍然有效)带有链接文档/文档中实际答案中的代码。

关于php - 如何统计一个目录下的文件个数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45136400/

相关文章:

performance - Laravel 用于复杂的浏览器游戏网站

php - Laravel 5.2 无法将跨域 jQuery 方法识别为 AJAX

php - 在动态创建的 div-tag php javascript 中回显

php - 无法向我的表中添加新元素

php - 为什么 mysql_query() 使用 SELECT 语句返回 TRUE?

laravel - 如何在 CENTOS 服务器上托管 laravel 5.2

php - 需要选择并显示特定用户编写的所有条目并对其进行计数

php - 如何通过 PHP 将最干净的 url 获取/读取到 Meta Canonical?

laravel - 每五分钟运行一次任务而不重叠?

php - Laravel 5.2 pdf mime 类型验证问题