martinmuzatko / filehandler
创建、读取、写入、删除文件等,请参阅 https://github.com/MartinMuzatko/FileHandler
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-10-02 10:57:52 UTC
README
虽然我很懒,但当我遇到PHP函数冗余时,我更喜欢创建自己的工具,而不是一次次地重新输入整个文件打开调用。
我现在已经使用了这个自制的FileHandler多年,仍然找不到一个可行的替代方案。
可选要求
请确保启用finfo (fileinfo) php扩展,以在FileHandler::getInfo中获取mimetype
和encoding
。
安装
通过Composer
php composer.phar require martinmuzatko/filehandler
或将其作为依赖项添加
"require": {
"martinmuzatko/filehandler": "*"
}
用法
包含文件或使用自动加载器。
use martinmuzatko\filehandler\File; use martinmuzatko\filehandler\Handler as FileHandler;
使用以下方法,您可以轻松地创建、写入、重命名、读取文件。虽然FileHandler是一组静态方法,但File提供了一种快速修改文件方便的方式。
FileHandler 示例
检查图像尺寸
$file = 'path/to/image.jpg'; $info = FileHandler::getInfo($file); if ($info->width > 1920 || $info->height > 1080) { echo 'File '.$info->basename.' is too big, resize it to 1920x1080'; }
File 示例
批量创建客户目录
$customers = ['01-jake', '02-mike', '03-francis', '04-martin', '05-jane']; foreach ($customers as $customer) { $file = new File($customer.'/info.json'); $file ->create() ->write('[{ title: "Read instructions."}]') ->chmod(0644); }
常量
- TAB
- 换行符 (LF)
- 回车符 (CR)
- Windows 组合 (CRLF)
方法
FileHandler (静态)
方法以静态方式调用(FileHandler::getInfo())
getInfo($file)
以对象形式返回文件信息示例
$info = FileHandler::getInfo($file); echo $info->writable;
返回以下信息
路径
- dirname
- basename
- extension
- filename
- path
尺寸
- width
- height
时间戳和其他属性
- created
- modified
- size
- type
- mimetype(仅当启用finfo时)
- encoding(仅当启用finfo时)
权限
- owner
- group
- perms
检查 - 布尔值
- writable
- readable
- exists
- isfile
- isdir
- islink
getAsArray($file)
以数组形式返回文件内容。
read($file)
如果文件存在,则返回文件作为字符串
write($file, $content, $seperator = CRLF)
使用给定的字符串或数组覆盖文件,如果成功则返回true。
create($file, $content = '')
创建文件但不会覆盖现有文件。
delete($file)
如果存在,则删除文件。
rename($old, $new)
将文件从旧路径重命名到新路径
appendFile($targetFile, $file, $seperator = CRLF)
使用给定的分隔符将$file追加到$targetFile。
appendLine($targetFile, $content, $seperator = CRLF)
使用定义的$seperator将$content追加到$targetFile。
clearFile($file)
清除文件中的任何内容
listFiles($path = '.', $includeFiles = true, $includeFolders = true)
以数组形式列出有效目录中的所有文件
File (方法链)
构造函数和某些其他方法接受以下任何一种
- File实例
- 字符串(路径)
- 资源(通过
fopen()
获取的句柄) - 任何其他类型的文件流
构造后,File包含通过FileHandler::getInfo()
可检索的任何信息,例如。
$f = new File('index.php'); $f->writable;
###create() 创建文件,不接受参数,文件通过构造函数输入:new File('path/to/file');
###copy($target)
###delete()
###rename($name)
###move($target) 将文件移动到所需位置,将自动创建所有新位置所需的目录示例
$f = new File('index.php'); $f->move('path/to/new/');
将index.php移动到path/to/new
,并将创建文件夹./path
、./path/to
和./path/to/new
###chmod($octet) 将文件权限从0111更改为0777 ###merge($target) 正在进行中
###read() 获取文件内容。调用此方法后无法再进行方法链。
###concat($content, $separator = CRLF) 将内容添加到文件中。
###write($content) 以覆盖方式写入(若想添加到文件而不是覆盖,请使用concat)
###find($lookup) 在目录中查找文件,启用正则表达式。可以通过以下方式获取:$file = new File(); $file->find(); $file->selection;
或 $file->getSelected()
####示例: #####按字符串查找
find('item.png')
#####按正则表达式查找
find('/[\w]*$/')
#####按属性 - 值对以及运算符查找
find(['mimetype' => 'image/png'])
- 查找所有MIME类型为image/png的文件
find(['size' => '<'.600*1024])
- 查找所有小于600KB的文件
find(['created' => '>1441000000'])
- 查找所有创建日期晚于2015年8月31日的文件
find(['mimetype' => '!directory'])
- 查找所有非目录文件
####运算符: <, >, <=, >=, ! ####属性由getInfo()使用
###get() 此方法用作find()、select()或new File()的最终方法以获取所做的选择。如果没有进行选择,则返回路径。
###select() 通过路径数组、Files数组或File Selections数组选择文件。您也可以混合这些####示例
$file->select('file.png');
$file->select(['file.png', 'another.jpg', 'file.avi']);
$file->select([['file.png', 'another.jpg'], 'file.avi']);
$file->select([$file->find(), 'customers/file.txt']);
$file->select($file);
任何数组或数组数组都将被遍历以创建一个一维数组,并保存到公共属性$selection。选择可以通过 get() 获取。