kappa / filesystem
轻松处理文件和目录的系统
v4.2.0
2014-05-25 21:44 UTC
Requires
- php: >= 5.3.3
- kappa/utils: ~1.0
- nette/finder: ~2.2
Requires (Dev)
- kappa/tester: 1.1.*
- nette/http: ~2.2
Suggests
- nette/http: For using FileUpload as File::upload
README
轻松处理文件和目录的系统
要求
- PHP 5.3.3 或更高版本
- Kappa\Utils 1.0.0
- Nette 框架 ~2.1
安装
使用 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()
- 返回 SplFileInfotoImage()
- 返回 Nette\Utils\Image
目录 API
getInfo()
- 返回 SplFileInfogetDirectories()
- 返回目录中的目录作为数组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