kappa/filesystem

轻松处理文件和目录的系统

v4.2.0 2014-05-25 21:44 UTC

This package is auto-updated.

Last update: 2024-09-04 19:12:55 UTC


README

轻松处理文件和目录的系统

要求

安装

使用 Composer 安装 Kappa/FileSystem 是最佳方式

$ composer require kappa/filesystem:@dev

用法

创建新的文件或目录

$file = File::create('path/to/file.txt', 'Hello wolrd!') // Create a new file with Hello world! text
$directory  = Directory::create('path/to/directory') // Create a new directory

打开文件和目录

$file = File::open('path/to/file'); // Open file
$dorectory = Directory::open('path/to/file'); // Open directory

通过FileUpload上传文件

// $fileUpload is instance of FileUpload from forms example...
$file = File::upload($fileUpload, 'path/to/save/file');

Nette\Utils\Image加载文件

$image = Image::fromFile('image.png');
$image->resize(10,10);
$file = File::fromImage($image, 'newImage.png');

创建一个新图像 'newImage',大小为 10x10 px,并返回 File 实例

如果您可以不创建新文件而与同一图像一起工作,则使用原始文件名作为第二个参数

文件 API

  • read() - 返回文件内容
  • overwrite(content) - 覆盖文件内容 (内容可以为 null 以清空文件)
  • clear() - 清空文件内容 (等同于 overwrite(null))
  • append(content, newLine = true) - 将文本追加到文件末尾
  • getInfo() - 返回 SplFileInfo
  • toImage() - 返回 Nette\Utils\Image

目录 API

  • getInfo() - 返回 SplFileInfo
  • getDirectories() - 返回目录中的目录作为数组 path => Directory
  • getFiles() - 返回目录中的文件作为数组 path => File
  • getContent() - 返回目录和文件作为数组 path => Directory|File

文件系统 API

  • remove(source) - 删除文件或目录,source 必须是 File 或 Directory 的实例
  • rename(source, new name, overwrite) - 重命名文件或目录。 source 必须是 File 或 Directory 的实例,并返回新对象实例
  • copy(source, target, overwrite) - 将源复制到目标,source 必须是 File 或 Directory 的实例,target 可以是字符串或 Directory 的实例。返回复制文件的实例
  • move(source, target, overwrite) - 与 copy() 相同,但在复制后删除源

示例

$file = File::create('file.txt');
$file = FileSystem::rename($file, 'superFile.txt');
$file->getInfo()->getBasename(); // Return superFile.txt