相关疑难解决方法(0)

计算目录php中的文件数量

我正在开发一个稍微新的项目.我想知道如何制作它,以便计算某个目录中有多少文件.

<div id="header">
<?php 
    $dir = opendir('uploads/'); # This is the directory it will count from
    $i = 0; # Integer starts at 0 before counting

    # While false is not equal to the filedirectory
    while (false !== ($file = readdir($dir))) { 
        if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
    }

    echo "There were $i files"; # Prints out how many were in the directory
?>
</div>
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止(从搜索).但它出现不正常?我添加了一些注释,所以随意删除它们,它们只是让我尽可能地理解它.

如果您需要更多信息或觉得我没有详细描述,请随时说明.

php directory file count

96
推荐指数
8
解决办法
15万
查看次数

使用php列出目录中的所有文件夹子文件夹和文件

请给我一个解决方案,使用php列出目录中的所有文件夹,子文件夹,文件.我的文件夹结构是这样的:

Main Dir
 Dir1
  SubDir1
   File1
   File2
  SubDir2
   File3
   File4
 Dir2
  SubDir3
   File5
   File6
  SubDir4
   File7
   File8
Run Code Online (Sandbox Code Playgroud)

我想获取每个文件夹中所有文件的列表.

在php中有任何shell脚本命令吗?

php directory

55
推荐指数
4
解决办法
13万
查看次数

使用PHP关联数组查找笛卡尔积

假设我有一个如下所示的数组:

Array
(
    [arm] => Array
        (
            [0] => A
            [1] => B
            [2] => C
        )
    [gender] => Array
        (
            [0] => Female
            [1] => Male
        )
    [location] => Array
        (
            [0] => Vancouver
            [1] => Calgary
        )
)
Run Code Online (Sandbox Code Playgroud)

如何在保留外部关联数组的键并在内部数组中使用它们的同时找到笛卡儿积?算法的结果应该是这样的:

Array
(
    [0] => Array
        (
            [arm] => A
            [gender] => Female
            [location] => Vancouver
        )

    [1] => Array
        (
            [arm] => A
            [gender] => Female
            [location] => Calgary
        )

    [2] => Array
        (
            [arm] => A
            [gender] => …
Run Code Online (Sandbox Code Playgroud)

php algorithm associative-array cartesian-product

48
推荐指数
5
解决办法
2万
查看次数

如何递归迭代PHP中的文件?

我已经设置了一个基本脚本,它发布了一系列路径来查找其中的模板文件; 目前它只搜索两个级别的深度,我遇到了一些麻烦,让我的头围绕逻辑​​进行广泛循环迭代所有子目录,直到长度为0.

所以,如果我有这样的结构:

./components
./components/template.html
./components/template2.html
./components/side/template.html
./components/side/template2.html
./components/side/second/template.html
./components/side/second/template2.html
./components/side/second/third/template.html
./components/side/second/third/template2.html
Run Code Online (Sandbox Code Playgroud)

它只搜索.html文件的"side"目录,理想情况下我希望它检查所有子目录和.html文件的传递目录.到目前为止,这是我的工作代码:

<?php
function getFiles($path){
    $dh  = opendir($path);
    foreach(glob($path.'/*.html') as $filename){
        $files[] = $filename;
    }
    if (isset($files)) {
        return $files;
    }
}
foreach ($_POST as $path) {
    foreach (glob($path . '/*' , GLOB_ONLYDIR) as $secondLevel) {
        $files[] = getFiles($secondLevel);
    }
    $files[] = getFiles($path);
}
sort($files);
print_r(json_encode($files));
?>
Run Code Online (Sandbox Code Playgroud)

php loops

15
推荐指数
2
解决办法
3840
查看次数

Glob 模式不匹配任何内容

注意:自创建此线程以来,该模式已从 PHP 文档中删除。

根据PHP 文档,该模式...递归地匹配所有子目录,但是当我尝试使用它时,没有匹配任何文件。

根据文档,glob自 PHP 5.1 以来没有改变,但如果重要的话,我使用的是 PHP 7.2.24 。

目录结构:

.
??? bar
?   ??? bar_file
??? foo
    ??? 1
    ?   ??? foo_1_file
    ??? foo_file
Run Code Online (Sandbox Code Playgroud)

PHP:

var_dump(glob('./.../*')); // prints array(0) {}
var_dump(glob('./.../foo_file')); // prints array(0) {}
Run Code Online (Sandbox Code Playgroud)

我知道有一个解决这个问题的方法,但我想知道是否有 PHP 本地解决方案,如果没有,为什么 PHP 参考文档有缺陷。

php directory glob

6
推荐指数
1
解决办法
472
查看次数