如何在php中计算文件夹中的文件数?
我想使用户能够在自己的文件夹中上载一些文件(图片)。但只有当文件夹中包含少于五张图片时,这才可能实现。如果已经有5张图片,脚本必须让用户知道他/她的文件夹已满。 所以,我想知道php中是否有计算文件夹中文件数量的函数。或者php中的任何其他方法可以做到这一点?提前谢谢 没有更简单的方法:使用和如何在php中计算文件夹中的文件数?,php,directory,Php,Directory,我想使用户能够在自己的文件夹中上载一些文件(图片)。但只有当文件夹中包含少于五张图片时,这才可能实现。如果已经有5张图片,脚本必须让用户知道他/她的文件夹已满。 所以,我想知道php中是否有计算文件夹中文件数量的函数。或者php中的任何其他方法可以做到这一点?提前谢谢 没有更简单的方法:使用和readdir()如下所示: <?php $images_extension_array = array("jpg","jpeg","gif","png"); $dir = "/path/to/u
readdir()
如下所示:
<?php
$images_extension_array = array("jpg","jpeg","gif","png");
$dir = "/path/to/user/folder";
$dir_resource = opendir($dir);
$file_count = 0;
while (($file = readdir($dir_resource)) !== false) { // scan directory
$extension_from = strrpos($file,"."); // isolate extension index/offset
if ($extension_from && in_array(substr($file,$extension_from+1), $images_extension_array))
$file_count ++; //if has extension and that extension is "associated" with an image, count
}
if ($number_of_files == %) {
//do stuff
}
没有更简单的方法:使用和readdir()
如下所示:
<?php
$images_extension_array = array("jpg","jpeg","gif","png");
$dir = "/path/to/user/folder";
$dir_resource = opendir($dir);
$file_count = 0;
while (($file = readdir($dir_resource)) !== false) { // scan directory
$extension_from = strrpos($file,"."); // isolate extension index/offset
if ($extension_from && in_array(substr($file,$extension_from+1), $images_extension_array))
$file_count ++; //if has extension and that extension is "associated" with an image, count
}
if ($number_of_files == %) {
//do stuff
}
使用文件系统发射器
,如图所示:
$dir = "/path/to/folder";
$fi = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
$fileCount = iterator_count($fi);
使用文件系统发射器
,如图所示:
$dir = "/path/to/folder";
$fi = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
$fileCount = iterator_count($fi);
你可以用
$nbFiles=count(scandir('myDirectory'))-2;
(-2表示删除“.”和“.”您可以使用
$nbFiles=count(scandir('myDirectory'))-2;
(-2表示删除“.”和“.”您可以让PHP为您找到文件…然后计算它们
$count = count(glob("$path_to_user_dir/*"));
你可以让PHP帮你找到文件,然后数一数
$count = count(glob("$path_to_user_dir/*"));
我真的很喜欢DOPSanswer,但它会返回文件、目录和符号链接的计数,这可能不是目标。如果只需要目录中本地文件的计数,可以使用:
$path = "/path/to/folder";
$fs = new FilesystemIterator($path);
foreach($fs as $file) {
$file->isFile() ? ++$filecount : $filecount;
}
我真的很喜欢DOPSanswer,但它会返回文件、目录和符号链接的计数,这可能不是目标。如果只需要目录中本地文件的计数,可以使用:
$path = "/path/to/folder";
$fs = new FilesystemIterator($path);
foreach($fs as $file) {
$file->isFile() ? ++$filecount : $filecount;
}
这个小函数是对我刚才发现的一些代码的修改,它还将计算所有子文件夹以及这些文件夹中的所有内容:
<?PHP
$folderCount = $fileCount = 0;
countStuff('.', $fileCount, $folderCount);
function countStuff($handle, &$fileCount, &$folderCount)
{
if ($handle = opendir($handle)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($entry)) {
echo "Folder => " . $entry . "<br>";
countStuff($entry, $fileCount, $folderCount);
$folderCount++;
} else {
echo "File => " . $entry . "<br>";
$fileCount++;
}
}
}
closedir($handle);
}
}
echo "<br>==============<br>";
echo "Total Folder Count : " . $folderCount . "<br>";
echo "Total File Count : " . $fileCount;
?>
注意:我还将发布原始代码,只计算父目录的文件和文件夹,而不是下面的子文件夹:
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($entry)) {
echo "Folder => " . $entry . "<br>";
countStuff($entry, $fileCount, $folderCount);
$folderCount++;
} else {
echo "File => " . $entry . "<br>";
$fileCount++;
}
}
}
echo "<br>==============<br>";
echo "Total Folder Count : " . $folderCount . "<br>";
echo "Total File Count : " . $fileCount;
closedir($handle);
}
if($handle=opendir('.')){
while(false!==($entry=readdir($handle))){
如果($entry!=“&&&$entry!=”){
if(is_dir($entry)){
echo“Folder=>”$entry.“
”;
countStuff($entry、$fileCount、$folderCount);
$folderCount++;
}否则{
echo“File=>”$entry.“
”;
$fileCount++;
}
}
}
回声“
===================
”;
回显“文件夹总数:.$folderCount.”
“;
echo“文件总数:”.$fileCount;
closedir($handle);
}
这个小功能是对我不久前发现的一些代码的修改,它还将计算所有子文件夹以及这些文件夹中的所有内容:
<?PHP
$folderCount = $fileCount = 0;
countStuff('.', $fileCount, $folderCount);
function countStuff($handle, &$fileCount, &$folderCount)
{
if ($handle = opendir($handle)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($entry)) {
echo "Folder => " . $entry . "<br>";
countStuff($entry, $fileCount, $folderCount);
$folderCount++;
} else {
echo "File => " . $entry . "<br>";
$fileCount++;
}
}
}
closedir($handle);
}
}
echo "<br>==============<br>";
echo "Total Folder Count : " . $folderCount . "<br>";
echo "Total File Count : " . $fileCount;
?>
注意:我还将发布原始代码,只计算父目录的文件和文件夹,而不是下面的子文件夹:
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
if (is_dir($entry)) {
echo "Folder => " . $entry . "<br>";
countStuff($entry, $fileCount, $folderCount);
$folderCount++;
} else {
echo "File => " . $entry . "<br>";
$fileCount++;
}
}
}
echo "<br>==============<br>";
echo "Total Folder Count : " . $folderCount . "<br>";
echo "Total File Count : " . $fileCount;
closedir($handle);
}
if($handle=opendir('.')){
while(false!==($entry=readdir($handle))){
如果($entry!=“&&&$entry!=”){
if(is_dir($entry)){
echo“Folder=>”$entry.“
”;
countStuff($entry、$fileCount、$folderCount);
$folderCount++;
}否则{
echo“File=>”$entry.“
”;
$fileCount++;
}
}
}
回声“
===================
”;
回显“文件夹总数:.$folderCount.”
“;
echo“文件总数:”.$fileCount;
closedir($handle);
}
?可能重复?可能重复Nice替代方法method@DonCallisto我想我在stackoverflow上看到了另一个类似问题的答案,从那以后我就使用了它,它在另一种方法中运行得很好method@DonCallisto我想我在stackoverflow上看到了另一个类似问题的答案,从那以后我就开始使用了,效果很好这是“insertusernamehere”的一个副本comment@Pierre字体很相似,是的。事实上,在我看到这些之前,我正准备加入一些关于如何只查找图像文件的信息。好吧,在我意识到在*尼克斯之前,通过扩展进行全球化会变得不确定。(例如,区分大小写的操作系统和/或文件系统不会选择whatever.JPG
;您必须包括大小写的每个组合,或者至少包括常见的组合。)它是“insertusernamehere”的副本comment@Pierre字体很相似,是的。事实上,在我看到这些之前,我正准备加入一些关于如何只查找图像文件的信息。好吧,在我意识到在*尼克斯之前,通过扩展进行全球化会变得不确定。(例如,区分大小写的操作系统和/或文件系统不会选择whatever.JPG
;您必须包括大小写的每个组合,或者至少包括常见的组合。)