kartik-v / yii2-filesystem
文件系统实用工具,用于管理文件和文件夹,包括读取、写入和追加文件
dev-master / 1.0.x-dev
2018-11-21 07:23 UTC
This package is auto-updated.
Last update: 2024-09-21 20:25:54 UTC
README
yii2-filesystem
文件系统实用工具,用于管理文件和文件夹,包括读取、写入和追加文件。它还包括一个Resumable
组件,通过resumable.js提供面向对象的后端来管理可恢复和分块文件上传。
安装
运行以下命令之一:
$ php composer.phar require kartik-v/yii2-filesystem "@dev"
或者添加以下内容到您的composer.json
文件的require
部分:
"kartik-v/yii2-filesystem": "@dev"
to the require
section of your composer.json
file.
用法
示例:创建文件夹实例并在其中搜索所有.csv文件
use kartik\filesystem\Folder; $dir = new Folder('/path/to/folder'); $files = $dir->find('.*\.csv');
现在您可以遍历文件,读取或写入/追加内容,或简单地删除文件
use kartik\filesystem\File; foreach ($files as $file) { $file = new File($dir->pwd() . DIRECTORY_SEPARATOR . $file); $contents = $file->read(); // $file->write('I am overwriting the contents of this file'); // $file->append('I am adding to the bottom of this file.'); // $file->delete(); // I am deleting this file $file->close(); // Be sure to close the file when you're done }
示例
use kartik\filesystem\Folder; use kartik\filesystem\File; /** * Create a new folder with 0755 permissions */ $dir = new Folder('/path/to/folder', true, 0755); /** * Create a new file with 0644 permissions */ $file = new File('/path/to/file.php', true, 0644); /** * addPathElement: Returns $path with $element added, with correct slash in-between. */ $path = Folder::addPathElement('/a/path/for', 'testing'); // $path equals /a/path/for/testing /** * cd: Change directory to $path. Returns false on failure. */ $folder = new Folder('/foo'); echo $folder->path; // Prints /foo $folder->cd('/bar'); echo $folder->path; // Prints /bar $false = $folder->cd('/non-existent-folder'); /** * chmod: Change the mode on a directory structure recursively. * This includes changing the mode on files as well. */ $dir = new Folder(); $dir->chmod('/path/to/folder', 0644, true, ['skip_me.php']); /** * copy: Recursively copy a directory. */ $folder1 = new Folder('/path/to/folder1'); $folder1->copy('/path/to/folder2'); // Will put folder1 and all its contents into folder2 $folder = new Folder('/path/to/folder'); $folder->copy([ 'to' => '/path/to/new/folder', 'from' => '/path/to/copy/from', // Will cause a cd() to occur 'mode' => 0755, 'skip' => ['skip-me.php', '.git'], 'scheme' => Folder::SKIP // Skip directories/files that already exist. ]); /** * create: Create a directory structure recursively. Can be used to create * deep path structures like /foo/bar/baz/shoe/horn */ $folder = new Folder(); if ($folder->create('foo' . DS . 'bar' . DS . 'baz' . DS . 'shoe' . DS . 'horn')) { // Successfully created the nested folders } /** * delete: Recursively remove directories if the system allows. */ $folder = new Folder('foo'); if ($folder->delete()) { // Successfully deleted foo and its nested folders } /** * find: Returns an array of all matching files in the current directory. */ // Find all .png in your webroot/img/ folder and sort the results $dir = new Folder(WWW_ROOT . 'img'); $files = $dir->find('.*\.png', true); /* Array ( [0] => cake.icon.png [1] => test-error-icon.png [2] => test-fail-icon.png [3] => test-pass-icon.png [4] => test-skip-icon.png ) */ /** * findRecursive: Returns an array of all matching files in and below the current directory. */ // Recursively find files beginning with test or index $dir = new Folder(WWW_ROOT); $files = $dir->findRecursive('(test|index).*'); /* Array ( [0] => /var/www/demo/index.php [1] => /var/www/demo/test.php [2] => /var/www/demo/img/test-skip-icon.png [3] => /var/www/demo/img/test-fail-icon.png [4] => /var/www/demo/img/test-error-icon.png [5] => /var/www/demo/img/test-pass-icon.png ) */ /** * read: Returns an array of the contents of the current directory. The returned * array holds two sub arrays: One of directories and one of files. */ // Recursively find files beginning with test or index $dir = new Folder(WWW_ROOT); $files = $dir->findRecursive('(test|index).*'); /* Array ( [0] => /var/www/demo/index.php [1] => /var/www/demo/test.php [2] => /var/www/demo/img/test-skip-icon.png [3] => /var/www/demo/img/test-fail-icon.png [4] => /var/www/demo/img/test-error-icon.png [5] => /var/www/demo/img/test-pass-icon.png ) */
许可证
yii2-filesystem在BSD-3-Clause许可证下发布。有关详细信息,请参阅捆绑的LICENSE.md
。