使用 PHP 返回文件夹内的文件总数

发布于 2024-07-06 22:28:59 字数 300 浏览 3 评论 0原文

有没有更好/更简单的方法来查找目录中的图像数量并将其输出到变量?

function dirCount($dir) {
  $x = 0;
  while (($file = readdir($dir)) !== false) {
    if (isImage($file)) {$x = $x + 1}
  }
  return $x;
}

这看起来是一个很长的方法,有没有更简单的方法?

注意: 如果文件是图像,则 isImage() 函数返回 true。

Is there a better/simpler way to find the number of images in a directory and output them to a variable?

function dirCount($dir) {
  $x = 0;
  while (($file = readdir($dir)) !== false) {
    if (isImage($file)) {$x = $x + 1}
  }
  return $x;
}

This seems like such a long way of doing this, is there no simpler way?

Note: The isImage() function returns true if the file is an image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

爱格式化 2024-07-13 22:29:00

查看 DirectoryIterator 的标准 PHP 库(又名 SPL):(

$dir = new DirectoryIterator('/path/to/dir');
foreach($dir as $file ){
  $x += (isImage($file)) ? 1 : 0;
}

仅供参考,有一个名为 iterator_count() 的未记录函数,但我想现在最好不要依赖它。并且您需要过滤掉像 .无论如何。)

Check out the Standard PHP Library (aka SPL) for DirectoryIterator:

$dir = new DirectoryIterator('/path/to/dir');
foreach($dir as $file ){
  $x += (isImage($file)) ? 1 : 0;
}

(FYI there is an undocumented function called iterator_count() but probably best not to rely on it for now I would imagine. And you'd need to filter out unseen stuff like . and .. anyway.)

蓝戈者 2024-07-13 22:29:00

这将为您提供目录中内容的计数。 我将把只计算图像的部分留给你,因为我快要摔倒了。

iterator_count(new DirectoryIterator('path/to/dir/'));

This will give you the count of what is in your dir. I'll leave the part about counting only images to you as I am about to fallll aaasssllleeelppppppzzzzzzzzzzzzz.

iterator_count(new DirectoryIterator('path/to/dir/'));
萌梦深 2024-07-13 22:29:00

我这样做:

$files = scandir($dir);
$x = count($files);
echo $x;

但它也计算 . 和 ..

i do it like this:

$files = scandir($dir);
$x = count($files);
echo $x;

but it also counts the . and ..

塔塔猫 2024-07-13 22:29:00

上述代码

$count = count(glob("*.{jpg,png,gif,bmp}"));

是最好的,但 {jpg,png,gif} 位只有在末尾附加 GLOB_BRACE 标志才有效:

$count = count(glob("*.{jpg,png,gif,bmp}", GLOB_BRACE));

The aforementioned code

$count = count(glob("*.{jpg,png,gif,bmp}"));

is your best best, but the {jpg,png,gif} bit will only work if you append the GLOB_BRACE flag on the end:

$count = count(glob("*.{jpg,png,gif,bmp}", GLOB_BRACE));
纸短情长 2024-07-13 22:29:00

你可以使用glob...

$count = 0;
foreach (glob("*.*") as $file) {
    if (isImage($file)) ++$count;
}

或者,我不确定这是否能满足你的需求,但你可以这样做:

$count = count(glob("*.{jpg,png,gif,bmp}"));

you could use glob...

$count = 0;
foreach (glob("*.*") as $file) {
    if (isImage($file)) ++$count;
}

or, I'm not sure how well this would suit your needs, but you could do this:

$count = count(glob("*.{jpg,png,gif,bmp}"));
悍妇囚夫 2024-07-13 22:29:00

您还可以通过扩展抽象 FilterIterator 类,利用 isImage 函数利用 SPL 来过滤 DirectoryIterator 的内容。

class ImageIterator extends FilterIterator {

    public function __construct($path)
    {
        parent::__construct(new DirectoryIterator($path));
    }

    public function accept()
    {
        return isImage($this->getInnerIterator());
    }
}

然后,您可以使用 iterator_count (或实现 Countable 接口并使用本机 count 函数)来确定图像的数量。 例如:

$images = new ImageIterator('/path/to/images');
printf('Found %d images!', iterator_count($images));

使用这种方法,根据您需要如何使用此代码,将 isImage 函数移动到 ImageIterator 类中以使所有内容整齐地包装可能更有意义放在一处。

You could also make use of the SPL to filter the contents of a DirectoryIterator using your isImage function by extending the abstract FilterIterator class.

class ImageIterator extends FilterIterator {

    public function __construct($path)
    {
        parent::__construct(new DirectoryIterator($path));
    }

    public function accept()
    {
        return isImage($this->getInnerIterator());
    }
}

You could then use iterator_count (or implement the Countable interface and use the native count function) to determine the number of images. For example:

$images = new ImageIterator('/path/to/images');
printf('Found %d images!', iterator_count($images));

Using this approach, depending on how you need to use this code, it might make more sense to move the isImage function into the ImageIterator class to have everything neatly wrapped up in one place.

白鸥掠海 2024-07-13 22:29:00

我使用以下命令来获取 Laravel 中一个目录中所有类型文件的计数

   $dir = public_path('img/');
    $files = glob($dir . '*.*');

    if ( $files !== false )
    {
        $total_count = count( $files );
        return $totalCount;
    }
    else
    {
        return 0;
    }

I use the following to get the count for all types of files in one directory in Laravel

   $dir = public_path('img/');
    $files = glob($dir . '*.*');

    if ( $files !== false )
    {
        $total_count = count( $files );
        return $totalCount;
    }
    else
    {
        return 0;
    }
苏佲洛 2024-07-13 22:29:00

您的答案似乎非常简单。 我想不出用 PHP 或 Perl 更短的方法。

如果您使用的是 Linux,您可能能够使用涉及 ls、wc 和 grep 的 system/exec 命令,具体取决于 isImage() 的复杂程度。

无论如何,我认为你所拥有的已经足够了。 您只需编写该函数一次。

Your answer seems about as simple as you can get it. I can't think of a shorter way to it in either PHP or Perl.

You might be able to a system / exec command involving ls, wc, and grep if you are using Linux depending how complex isImage() is.

Regardless, I think what you have is quite sufficient. You only have to write the function once.

零度℉ 2024-07-13 22:29:00

我用它来返回目录中除 . 和..

return count(glob("/path/to/file/[!\.]*"));

这是用于文件匹配目的的一个很好的全局过滤器列表

I use this to return a count of ALL files in a directory except . and ..

return count(glob("/path/to/file/[!\.]*"));

Here is a good list of glob filters for file matching purposes.

温暖的光 2024-07-13 22:29:00
$nfiles = glob("/path/to/file/[!\\.]*");

if ($nfiles !== FALSE){

    return count($nfiles);

} else {

    return 0;

}
$nfiles = glob("/path/to/file/[!\\.]*");

if ($nfiles !== FALSE){

    return count($nfiles);

} else {

    return 0;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文