我想知道如何使用 Laravel 5.2 计算目录中的文件数:
$folderPath="upload/";
$countFile=0;
$totalFiles=glob($folderPath."*");
if($totalFiles){
$countFile=count($totalFiles);
}
print_r($countFile);
这对我不起作用。
最佳答案
根据 5.6 documentation,您可以使用 files
或 allFiles
计算目录中的文件数(不过,此功能自 5.0 以来一直存在,而您的问题专门针对 5.2)。
files
和allFiles
的区别在于allFiles
与files
不同,它会递归搜索子目录。同样,根据文档:
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/