popphp/pop-dir

Pop PHP 框架的目录遍历组件

4.0.0 2023-11-09 04:39 UTC

This package is auto-updated.

Last update: 2024-09-09 06:16:11 UTC


README

Build Status Coverage Status

Join the chat at https://popphp.slack.com Join the chat at https://discord.gg/TZjgT74U7E

概述

pop-dir 是一个用于轻松遍历目录中文件和子目录的组件。

它是 Pop PHP 框架 的一个组件。

顶部

安装

使用 Composer 安装 pop-dir

composer require popphp/pop-dir

或者,在您的 composer.json 文件中引入它

"require": {
    "popphp/pop-dir" : "^4.0.0"
}

顶部

快速入门

遍历目录
use Pop\Dir\Dir;

$dir = new Dir('my-dir');

foreach ($dir->getFiles() as $file) {
    echo $file;
}

如果您想递归遍历目录并获取每个文件的完整路径。

use Pop\Dir\Dir;

$dir = new Dir('my-dir', [
    'absolute'  => true,
    'recursive' => true
]);

foreach ($dir->getFiles() as $file) {
    echo $file;
}

顶部

选项

可选的布尔选项有 $options 数组参数

$options = [
    'absolute'  => true,  // store the absolute, full path of the items in the directory
    'relative'  => false  // store the relative path of the items in the directory
    'recursive' => true,  // traverse the directory recursively
    'filesOnly' => false, // store only files in the object (and not other directories)
];

absoluterelative 选项不能同时使用。

如果 absolute 设置为 true,它将返回文件和目录的绝对路径

'/home/path/file1.txt`
'/home/path/file2.txt`

如果 relative 设置为 true,它将返回文件和目录的相对路径

'path/file1.txt`
'path/file2.txt`

如果没有传递任何选项,它将只返回基本文件名和目录名

'file1.txt`
'file2.txt`

顶部

可以使用 emptyDir() 方法清空目录

use Pop\Dir\Dir;

$dir = new Dir('my-dir');
$dir->emptyDir();

true 标志将同时删除实际目录(请谨慎使用)

$dir->emptyDir(true);

顶部