Effectra 文件系统 (fs) 包。

v1.1.0 2023-10-01 16:40 UTC

This package is auto-updated.

Last update: 2024-09-30 02:16:04 UTC


README

Effectra FS 是一个 PHP 库,提供了一套完整的类来管理文件系统的各个方面,包括文件、文件夹、JSON、CSV 和 XML。Effectra FS 提供的 File 类提供了处理文件相关操作的静态方法。

安装

composer require effectra/fs

此命令将下载并安装 Effectra FS 库及其依赖项到您的项目中。

安装完成后,您可以通过包含必要的文件并利用前面 readme 文件中展示的 File 类来开始使用此库。

类参考

File

File 类提供用于管理文件和执行文件系统操作的静态方法。

方法

  • append($path, $data):向文件追加数据。
  • exists($path):检查文件是否存在。
  • delete($paths):删除一个或多个文件。
  • put($path, $contents, $lock = false):将内容写入文件。
  • replaceInFile($search, $replace, $path):在文件中替换字符串。
  • clear($path):清除文件内容。
  • getContent($path, ?int $length = null):检索文件内容。
  • search($search, $path):在文件中搜索字符串。
  • lastModified($path):获取文件的最后修改时间。
  • isExecutable($path):检查文件是否可执行。
  • isUploadedFile($path):检查文件是否为上传文件。
  • isReadable($path):检查文件是否可读。
  • isWritable($path):检查文件是否可写。
  • isFile($file):检查路径是否为常规文件。
  • glob($pattern, $flags = 0):查找与模式匹配的路径名。
  • extension($path):获取路径的文件扩展名。
  • type($path):获取路径的文件类型。
  • mimeType($path):获取文件的 MIME 类型。
  • basename($path):获取路径的基本名称。
  • name($path):获取路径的文件名(不包含扩展名)。
  • move($path, $target):将文件移动到新位置。
  • copy($path, $target):将文件复制到新位置。
  • size($path):获取文件大小。
  • require($path):包含 PHP 文件。
  • requireOnce($path):只包含一次 PHP 文件。
  • lines($path):读取文件并返回行数组。
  • hash($path, $algorithm = 'md5'):计算文件的哈希值。
  • replace($path, $content, $mode = null):替换文件的内容。
  • chmod($path, $mode = null):更改文件的模式。
  • perms($path):获取文件的权限。
  • fileATime($path):获取文件的最后访问时间。
  • fileCTime($path):获取文件的创建时间。
  • fileGroup($path):获取文件的组 ID。
  • fileOwner($path):获取文件所有者的用户 ID。

用法

使用 Composer 安装 Effectra FS 库后,您可以在 PHP 代码中开始使用 File 类。以下是如何利用 File 类提供的某些方法的示例:

<?php

require_once 'vendor/autoload.php';

use Effectra\Fs\File;

// Example usage of the `exists()` method
$fileExists = File::exists('path/to/file.txt');
if ($fileExists) {
    echo "The file exists.";
} else {
    echo "The file does not exist.";
}

// Example usage of the `put()` method
$fileContent = 'Hello, world!';
$writeSuccess = File::put('path/to/newfile.txt', $fileContent);
if ($writeSuccess !== false) {
    echo "The file was successfully written.";
} else {
    echo "Failed to write the file.";
}

// Example usage of the `getContent()` method
$fileContent = File::getContent('path/to/file.txt');
echo "The file content is: " . $fileContent;

// Example usage of the `delete()` method
$deleteSuccess = File::delete('path/to/file.txt');
if ($deleteSuccess) {
    echo "The file was successfully deleted.";
} else {
    echo "Failed to delete the file.";
}

// ... Continue using other methods provided by the `File` class

?>

在此示例中,我们演示了如何使用 exists()put()getContent()delete() 等方法。您可以探索类中可用的各种方法,并根据您的文件系统需求使用它们。

请确保根据您的具体使用情况调整文件路径。

请记住在您的PHP脚本开头包含Composer生成的自动加载器(require_once 'vendor/autoload.php';),以确保Effectra FS库被正确加载。

Effectra FS - 目录类

用于处理目录的实用类。

方法

  • isDirectory(string $directory): bool:检查给定路径是否为目录。
  • make(string $path, int $mode = 0755, bool $recursive = false, bool $force = false): bool:创建一个新的目录。
  • delete(string $directory, bool $preserve = false): bool:删除一个目录。
  • deleteDirectories(string $directory): bool:删除目录中的所有子目录。
  • instance(string $directory):获取目录实例。
  • copy(string $source, $destination):递归地将目录复制到目标位置。
  • name(string $directory):从路径中检索目录名称。
  • rename(string $from, string $to, $context = null): bool:重命名目录。
  • fullName($directory):检索目录父级的完整路径。
  • read($directory):检索目录中的文件和子目录。
  • parent($directory, $levels = 1):检索给定目录路径的父目录。
  • isPrivate($directory):检查目录是否被认为是私有的。
  • files($directory, $full_path = false, $only_extension = false):检索目录中的文件。
  • hasFiles($directory):检查目录是否有文件。
  • deleteFiles($directory, $only_extension = false):删除目录中的文件。
  • directories($directory, $full_path = false):检索目录中的子目录。
  • empty($directory):通过删除文件和子目录来清空目录。

示例用法

use Effectra\Fs\Directory;

// Check if a path is a directory
$isDirectory = Directory::isDirectory('/path/to/directory');

// Create a new directory
$created = Directory::make('/path/to/new_directory');

// Delete a directory
$deleted = Directory::delete('/path/to/directory');

// Delete all directories within a directory
$deletedAll = Directory::deleteDirectories('/path/to/parent_directory');

// Get a directory instance
$dirInstance = Directory::instance('/path/to/directory');

// Copy a directory recursively to a destination
$copied = Directory::copy('/path/to/source_directory', '/path/to/destination_directory');

// Retrieve the name of a directory
$directoryName = Directory::name('/path/to/directory');

// Rename a directory
$renamed = Directory::rename('/path/to/old_directory', '/path/to/new_directory');

// Retrieve the full path of a directory's parent
$parentDirectory = Directory::fullName('/path/to/directory');

// Retrieve the files and directories within a directory
$entries = Directory::read('/path/to/directory');

// Retrieve the parent directory of a given directory path
$parentDirectory = Directory::parent('/path/to/directory');

// Check if a directory is considered private
$isPrivate = Directory::isPrivate('/path/to/directory');

// Retrieve the files within a directory
$files = Directory::files('/path/to/directory', true, ['txt', 'csv']);

// Check if a directory has any files
$hasFiles = Directory::hasFiles('/path/to/directory');

// Delete files within a directory
$deletedFiles = Directory::deleteFiles('/path/to/directory', ['txt', 'csv']);

// Retrieve the directories within a directory
$directories = Directory::directories('/path/to/directory', true);

// Empty a directory by deleting its files and subdirectories
Directory::empty('/path/to/directory');

注意:请确保在您的代码中将/path/to替换为实际的路径。

Effectra FS - 路径类

Effectra\Fs\Path 是一个用于在PHP中操作文件路径的实用类。

用法

Path 类提供了一些有用的方法来操作文件路径

ds(): string

获取当前平台上的目录分隔符。

示例

$separator = \Effectra\Fs\Path::ds();
echo $separator; // Outputs '\' on Windows, '/' on Unix-like systems

format(string $path): string

通过替换正斜杠、反斜杠和多个分隔符,格式化给定的路径。

示例

$path = \Effectra\Fs\Path::format('path/to//file');
echo $path; // Outputs 'path/to/file' on all platforms

removeExtension(string $path): string

从给定的路径中删除扩展名。

示例

$path = \Effectra\Fs\Path::removeExtension('file.txt');
echo $path; // Outputs 'file'

setExtension(string $path, string $ext): string

设置给定路径的扩展名。

示例

$path = \Effectra\Fs\Path::setExtension('file', 'txt');
echo $path; // Outputs 'file.txt'

Effectra FS - FileData 类

Effectra\Fs\FileData 是一个用于在PHP中将文件转换为数据URL及其相反操作的实用类。

##使用方法

FileData 类提供两种方法将文件转换为数据URL及其相反操作

fileToDataUrl(string $file): string|false

将文件转换为数据URL。

  • 参数

    • $file (string):文件的路径。
  • 返回

    • 表示文件数据URL的字符串,或者在转换失败时返回 false

示例

$fileData = new \Effectra\Fs\FileData();
$dataUrl = $fileData->fileToDataUrl('/path/to/file.jpg');
if ($dataUrl !== false) {
    echo $dataUrl;
} else {
    echo "Failed to convert the file to a data URL.";
}

dataUrlToFile(string $dataUrl, string $file): bool

将数据URL转换为文件。

  • 参数

    • $dataUrl (string):要转换的数据URL。
    • $file (string):保存文件的路径。
  • 返回

    • 如果文件成功保存,则返回 true,否则返回 false

示例

$fileData = new \Effectra\Fs\FileData();
$result = $fileData->dataUrlToFile('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAAAAAAAD...', '/path/to/output.jpg');
if ($result) {
    echo "File saved successfully.";
} else {
    echo "Failed to save the file.";
}

文件加密类(版本 >= 1.1)

FileEncryption 类提供加密和解密文件的实用方法。

用法

use Effectra\Fs\FileEncryption;

// Create an instance of FileEncryption with the encryption key
$fileEncryption = new FileEncryption('your-encryption-key');

// Encrypt a file
$fileEncryption->encryptFile('path/to/source/file.txt', 'path/to/destination/encrypted-file.txt');

// Decrypt a file
$fileEncryption->decryptFile('path/to/source/encrypted-file.txt', 'path/to/destination/decrypted-file.txt');

请确保将 'your-encryption-key' 替换为您自己的加密密钥。

示例

use Effectra\Fs\FileEncryption;

$fileEncryption = new FileEncryption('my-secret-key');

// Encrypt a file
$fileEncryption->encryptFile('data.txt', 'encrypted-data.txt');

// Decrypt a file
$fileEncryption->decryptFile('encrypted-data.txt', 'decrypted-data.txt');

在上面的示例中,使用 FileEncryption 类来加密 data.txt 文件的内容,并将加密数据存储在 encrypted-data.txt 文件中。它还提供了一个方法来解密加密数据并将其保存到单独的文件中。

加密算法

FileEncryption类使用AES-256-CBC加密算法,并为每次加密操作生成一个随机初始化向量(IV)。

注意:请确保加密密钥的安全,不要与未经授权的人员共享。

贡献

欢迎贡献!如果您发现任何问题或有改进建议,请在GitHub仓库中提交问题或拉取请求。

许可证

本软件包是开源软件,许可协议为MIT协议。