gspataro / filesystem
一个PHP文件系统组件
1.0.0
2023-03-11 16:00 UTC
Requires
- php: >=8.0
Requires (Dev)
- mikey179/vfsstream: 1.6.*
- phpunit/phpunit: 9.5.*
README
一个用于轻松管理您应用程序文件和目录的组件。
安装
需要 PHP 8.0+
使用 Composer 需要该组件
composer require gspataro/filesystem
快速开始
要开始使用FileSystem组件,您只需要初始化Storage类。这将使您能够轻松访问应用程序文件系统中的文件和目录。
<?php use GSpataro\FileSystem\Storage; /** * The storage class gives you easy access to files and directories in your root folder * * @param string $root The path of your root */ $storage = new Storage(__DIR__); /** * Open a file * * @param string $path The path to the file (exluding root) */ $storage->openFile("document.txt"); // Will open: __DIR__ . '/document.txt' /** * Open a directory * * @param string $path The path to the directory (exluding root) */ $storage->openDir("directory"); // Will open: __DIR__ . '/directory'
文件和目录类
您还可以在存储类之外实例化File和Directory类。在某些情况下,这将为您提供更大的灵活性。这两个类都有一些共同的方法
<?php use GSpataro\FileSystem\File; use GSpataro\FileSystem\Directory; $file = new File(__DIR__ . '/document.txt'); $directory = new Directory(__DIR__ . '/document'); /** * Verify if the file/directory exists */ $file->exists(); $directory->exists(); // This variant throws an exeption if the file/directory does not exist $file->existsOrDie(); $directory->existsOrDie(); /** * Write or create a file * * @param mixed $content The content to write * @param bool $overwrite = true If true, overwrite the existing content, otherwise add the new content at the end of the file */ $file->write("Lorem ipsum"); /** * Create a directory * * @param bool $recursive = false If true, create all the non existing directories present in the path * @param bool $permissions = 0777 Define the permissions of the directory */ $directory->write(); /** * Read a file/directory * * File: return the content of the file * Directory: return the content of the directory as File/Directory objects contained in an array */ $file->read(); $directory->read(); /** * Delete a file */ $file->delete(); /** * Delete a directory * * @param bool $recursive = false If true, delete the directory and its content, otherwise delete the directory only if empty */ $directory->delete(true); /** * Move/copy a file/directory * * Directory: this will also move/copy the content of the directory * @param bool $overwrite = false If true, overwrite the new path */ $file->move(__DIR__ . '/moved_document.txt'); $file->copy(__DIR__ . '/copied_documen.txt'); $directory->move(__DIR__ . '/moved_directory'); $directory->copy(__DIR__ . '/copied_directory');
仅目录方法
<?php /** * Verify if the directory is empty * * @return bool */ $directory->empty();
仅文件方法
<?php /** * Verify if the file matches one or more extensions * * @param string|array $extensions * @return bool */ $file->matchExtensions("txt"); // This variant throws an exception if the file doesn't match the given extension/s $file->matchExcentionsOrDie(["txt", "md"]); /** * Require PHP file * * @return mixed */ $file->import();