递归树文件夹函数 php 不进入子文件夹
Recursive tree folder function php not going into subfolders
我目前正在开发 php 文件索引器,我需要创建一个递归函数来创建一个数组,该数组将包含父文件夹的文件和子文件夹列表,子文件夹也是包含的数组他们的文件和他们的子文件夹(等...)。由于这是一个学校项目,我不能使用 DirectoryRecursiveIterator 及其兄弟 RecursiveIterator 和 DirectoryIterator。
我的问题是它扫描父文件夹并找到子文件夹和文件,但不会进入子文件夹以查找文件和子文件夹。
代码
<?php
class H5AI
{
// Properties
private $_tree;
private $_path;
// Construct
public function __construct($_path)
{
$_tree = [];
$parent = $_tree;
print_r($this->getFiles($_path, $parent));
}
// Methods
public function getPath()
{
return $this->_path;
}
public function getTree()
{
return $this->_tree;
}
public function getFiles($path, $parent)
{
//Opening the directory
$dirHandle = opendir($path);
while (false !== $entry = readdir($dirHandle)) {
//If file found
if (!is_dir($path . DIRECTORY_SEPARATOR . $entry)) {
array_push($parent, $entry);
}
// When subdirs found (ignore . & ..)
else if (is_dir($path . DIRECTORY_SEPARATOR . $entry) && $entry !== "." && $entry !== "..") {
$newPath = $path . DIRECTORY_SEPARATOR . $entry;
$parent[$entry] = [];
$this->getFiles($newPath, $parent[$entry]);
}
}
return $parent;
}
}
// Calling function
$h5a1 = new H5AI($argv[1]);
// Command I use in the terminal
php index.php "./test_dir"
//Output
Array
(
[sub_test_dir] => Array
(
)
[0] => test.css
[sub_test_dir2] => Array
(
)
[1] => test.js
[2] => test.html
)
您正在创建一个单独的数组,其中包含您想要的内容,但没有插入到您的父数组中。你一切都好,你只需要一点点修复:
//...
else if (is_dir($path . DIRECTORY_SEPARATOR . $entry) && $entry !== "." && $entry !== "..") {
$newPath = $path . DIRECTORY_SEPARATOR . $entry;
$parent[$entry] = [];
$parent[$entry] = $this->getFiles($newPath, $parent[$entry]); // <-- fix is on this line
}
class H5AI {
public function __construct(string $path) {
print_r($this->getFiles($path));
}
public function getFiles(string $directory): array {
$handle = opendir($directory);
$entries = [];
while (true) {
$entry = readdir($handle);
if ($entry === false) {
break;
}
$path = $directory . DIRECTORY_SEPARATOR . $entry;
if (is_file($path) && !str_starts_with($entry, '.')) {
$entries[] = $entry;
} elseif (is_dir($path) && !in_array($entry, [ '.', '..', '$RECYCLE.BIN' /* add other dir names to exclude here */ ])) {
$entries[$entry] = $this->getFiles($path);
}
}
closedir($handle);
return $entries;
}
}
我目前正在开发 php 文件索引器,我需要创建一个递归函数来创建一个数组,该数组将包含父文件夹的文件和子文件夹列表,子文件夹也是包含的数组他们的文件和他们的子文件夹(等...)。由于这是一个学校项目,我不能使用 DirectoryRecursiveIterator 及其兄弟 RecursiveIterator 和 DirectoryIterator。 我的问题是它扫描父文件夹并找到子文件夹和文件,但不会进入子文件夹以查找文件和子文件夹。
代码
<?php
class H5AI
{
// Properties
private $_tree;
private $_path;
// Construct
public function __construct($_path)
{
$_tree = [];
$parent = $_tree;
print_r($this->getFiles($_path, $parent));
}
// Methods
public function getPath()
{
return $this->_path;
}
public function getTree()
{
return $this->_tree;
}
public function getFiles($path, $parent)
{
//Opening the directory
$dirHandle = opendir($path);
while (false !== $entry = readdir($dirHandle)) {
//If file found
if (!is_dir($path . DIRECTORY_SEPARATOR . $entry)) {
array_push($parent, $entry);
}
// When subdirs found (ignore . & ..)
else if (is_dir($path . DIRECTORY_SEPARATOR . $entry) && $entry !== "." && $entry !== "..") {
$newPath = $path . DIRECTORY_SEPARATOR . $entry;
$parent[$entry] = [];
$this->getFiles($newPath, $parent[$entry]);
}
}
return $parent;
}
}
// Calling function
$h5a1 = new H5AI($argv[1]);
// Command I use in the terminal
php index.php "./test_dir"
//Output
Array
(
[sub_test_dir] => Array
(
)
[0] => test.css
[sub_test_dir2] => Array
(
)
[1] => test.js
[2] => test.html
)
您正在创建一个单独的数组,其中包含您想要的内容,但没有插入到您的父数组中。你一切都好,你只需要一点点修复:
//...
else if (is_dir($path . DIRECTORY_SEPARATOR . $entry) && $entry !== "." && $entry !== "..") {
$newPath = $path . DIRECTORY_SEPARATOR . $entry;
$parent[$entry] = [];
$parent[$entry] = $this->getFiles($newPath, $parent[$entry]); // <-- fix is on this line
}
class H5AI {
public function __construct(string $path) {
print_r($this->getFiles($path));
}
public function getFiles(string $directory): array {
$handle = opendir($directory);
$entries = [];
while (true) {
$entry = readdir($handle);
if ($entry === false) {
break;
}
$path = $directory . DIRECTORY_SEPARATOR . $entry;
if (is_file($path) && !str_starts_with($entry, '.')) {
$entries[] = $entry;
} elseif (is_dir($path) && !in_array($entry, [ '.', '..', '$RECYCLE.BIN' /* add other dir names to exclude here */ ])) {
$entries[$entry] = $this->getFiles($path);
}
}
closedir($handle);
return $entries;
}
}