php 计算php文件夹中的文件数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14194173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:50:14  来源:igfitidea点击:

Count number of files in folder in php

php

提问by Rohit Goel

<?php 
$directory = '/var/www/ajaxform/';
if (glob($directory . '.jpg') != false)
{
    $filecount = count(glob($directory . '*.jpg'));
    echo $filecount;
}
else
{
    echo 0;
}
?>

there are four jpg images in this directory but it returns 0

此目录中有四个 jpg 图像,但它返回 0

回答by Michel Feldheim

Globreturns an array, on error it returns false.

Glob返回array,出错时返回false

Try this:

尝试这个:

$directory = '/var/www/ajaxform/';
$files = glob($directory . '*.jpg');

if ( $files !== false )
{
    $filecount = count( $files );
    echo $filecount;
}
else
{
    echo 0;
}

回答by karlingen

Try this:

尝试这个:

<?php 
$directory = '/var/www/ajaxform/';
if (glob($directory . '*.jpg') != false)
{
 $filecount = count(glob($directory . '*.jpg'));
 echo $filecount;
}
else
{
 echo 0;
}
?>

回答by kokx

There is a mistake in your glob pattern (in the if). You are missing a *:

您的 glob 模式中有错误(在 if 中)。您缺少一个 *:

glob($directory . '*.jpg')

should work

应该管用

回答by sbrbot

Minimalization approach:

最小化方法:

function getImagesNo($path)
{
  return ($files=glob($path.'*.jpg')) ? count($files) : 0;
}

回答by jimf

glob is case sensitive, according to the PHP docs. Are your extensions lowercase? Does the executing account have access to /var/www/ajaxform/?

根据 PHP 文档,glob 区分大小写。你的扩展名是小写的吗?执行帐户是否可以访问 /var/www/ajaxform/?

回答by Suresh Kamrushi

Just try this--

试试这个——

if (glob($directory . "*.jpg") != false)
$filecount = count(glob($directory . "*.jpg"));
else
$filecount = 0;