【问题标题】:Count array elements returned by scandir(); PHP [duplicate]计算scandir()返回的数组元素; PHP [重复]
【发布时间】:2016-01-06 13:12:56
【问题描述】:

我在文件夹“文件”中有一些文件夹。我想通过我的 php 代码返回这个文件夹的数量:

$x = count(scandir('/files'));
echo $x;

但这不起作用。怎么了?

【问题讨论】:

  • var_dump($x) 的结果是什么?
  • apache 用户是否对“/files”文件夹具有必要的权限?
  • “这不起作用” 是什么意思?错误?计数错误?空输出?

标签: php count scandir


【解决方案1】:

如果您在 files 文件夹中有一些文件,这是解决方案。

  $directory = 'files/';
  $scanned_directory = array_diff(scandir($directory), array('..', '.'));//to remove dots
  $x = count($scanned_directory);
  echo $x;

尽量不要使用 /files。它将查找名为 files

的文件

【讨论】:

    【解决方案2】:

    试试这个:

    $directory = '/your/directory/path/';
    $files = glob($directory . '*.*');   // returns an array on success and false on error.
    
    if ( $files !== false )
    {
        $filecount = count( $files );
        echo $filecount;
    }
    else
    {
        echo 0;
    }
    

    【讨论】:

      【解决方案3】:

      最好先检查目录是否存在:

      $directory = '/your/directory/path/';
      if(!is_dir($directory))
          die("direction not exists");
      

      ant 然后计数并删除 ... 元素

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-21
        • 2020-12-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多