静态方法
本类提供了一系列与文件系统及文件处理相关的方法,基本可以满足基本编程需要。
获取根目录
本方法的调用模式为:myFile::rootPath($mode = false)
获取当前网站根目录路径,$mode 用于指定返回的路径间隔符固定为“/”还是依照当前系统模式
真实路径
本方法的调用模式为:myFile::realPath($file, $mode = false)
返回某一网站相对路径所对应的真是路径,$mode 用于指定返回的路径间隔符固定为“/”还是依照当前系统模式
建立目录
本方法的调用模式为:myFile::mkdir($dir)
建立多层级目录
文件类型
本方法的调用模式为:myFile::getMime($file)
返回文件 MimeType 信息
文件属性
本方法的调用模式为:myFile::getAttrib($file, $show_txt = false)
返回文件属性信息,默认为“777”的格式,如果 $show_txt 为 true,则依照“rwx--r--r”的模式显示
设置属性
本方法的调用模式为:myFile::setAttrib($file, $attrib)
设置指定文件的属性,属性值按照“0777”的八进制模式
响应头信息
本方法的调用模式为:myFile::getHeader($url, $mode = true)
返回向远程文件发出调用请求时,远程服务器所回馈的响应头
文件大小
本方法的调用模式为:myFile::getSize($file, $format = ture)
返回文件大小,如果 $format 设定为 true,返回如“12.34MB”格式,否则直接返回字节数
复制文件
本方法的调用模式为:myFile::copy($source, $dest, $overwrite = false)
复制指定文件或目录到指定路径
删除文件
本方法的调用模式为:myFile::del($file)
删除指定文件或目录
重命名文件
本方法的调用模式为:myFile::rename($file, $newname)
将某一文件或目录重命名,同时也能起到移动文件或目录的作用
保存文件
本方法的调用模式为:myFile::saveFile($file, $content='', $mode='wb')
将某一数据存储到指定文件中
获取本地文件
本方法的调用模式为:myFile::getLocal($file, $length=0, $offset=0)
获取本地文件的全部或部分内容
获取网络文件
本方法的调用模式为:myFile::getRemote($url, $header, $method, $data, $timeout)
或 myFile::getRemote_curl($url, $data='', $header=array())
以上两种方法通过不同模式获取远程服务器文件内容
查找文件
本方法的调用模式为:myFile::find($filter='', $dir='./', $recursive=false)
在指定目录下查找指定文件,可带 * 等通配符
下载文件
本方法的调用模式为:myFile::getRemoteFile($remote_file, $local_file)
将远程文件存储到本地
目录树
本方法的调用模式为:myFile::getTree($dir='./')
获取某一路径的文件、目录结构树
可写判断
本方法的调用模式为:myFile::rewritable($file)
判断指定文件是否可写
实例模式
除了通用的静态模式,本类还提供了针对某一文件的实例模式,代码如下:
$f = new myFile('path/to/file.ext');
echo '<pre>';
echo 'Content : '.$f."\n\n";
var_dump($f->info, $f->headers, $f->get());
echo '</pre>';
以上代码为 'path/to/file.ext' 建立了一个文件实例,这个文件可以是本地文件,也可以是远程文件,以本地文件为例,$f->info 将返回如下内容:
array(11) {
["type"]=>
string(4) "file"
["is_writable"]=>
bool(true)
["time_visit"]=>
int(1612172685)
["time_edit"]=>
int(1612164477)
["size"]=>
int(0)
["perms"]=>
string(4) "0644"
["mime"]=>
string(10) "text/plain"
["path"]=>
string(45) "/path/to/file"
["fullname"]=>
string(8) "utf8.txt"
["name"]=>
string(4) "utf8"
["ext"]=>
string(3) "txt"
}
其中所有的项目都可以通过 $f->name 的方式来实现,name 就是以上数组的字符串下标。
如果实例文件为网络文件,还可以查看服务器相关的响应头,如:
$f = new f('https://www.baidu.com');
var_dump($f->headers);
将返回如下内容:
array(13) {
[0]=>
string(15) "HTTP/1.0 200 OK"
["accept-ranges"]=>
string(5) "bytes"
["cache-control"]=>
string(8) "no-cache"
["content-length"]=>
string(5) "14722"
["content-type"]=>
string(9) "text/html"
["date"]=>
string(29) "Sun, 28 Feb 2021 08:35:34 GMT"
["p3p"]=>
array(2) {
[0]=>
string(34) "CP=" OTI DSP COR IVA OUR IND COM ""
[1]=>
string(34) "CP=" OTI DSP COR IVA OUR IND COM ""
}
["pragma"]=>
string(8) "no-cache"
["server"]=>
string(7) "BWS/1.1"
["set-cookie"]=>
array(4) {
[0]=>
string(129) "BAIDUID=E3353D3F50EE0C7905218850E62437C5:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"
[1]=>
string(125) "BIDUPSID=E3353D3F50EE0C7905218850E62437C5; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"
[2]=>
string(99) "PSTM=1614501334; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com"
[3]=>
string(150) "BAIDUID=E3353D3F50EE0C79D4F06D27732AA0AC:FG=1; max-age=31536000; expires=Mon, 28-Feb-22 08:35:34 GMT; domain=.baidu.com; path=/; version=1; comment=bd"
}
["traceid"]=>
string(39) "161450133403746142826945050894402088022"
["vary"]=>
string(15) "Accept-Encoding"
["x-ua-compatible"]=>
string(16) "IE=Edge,chrome=1"
}
获取文件内容可通过 $f->get() 方法,或直接 echo $f; 也可自动调用此方法显示文件内容;也可以直接通过更改 $f->content 的内容,然后通过 $f->save() 方法将更改后的内容保存回本地文件,或者也可以直接将修改内容作为参数一传递给 save 方法。
别名调用
为便于开发,本类默认别名定义如下:
$func_alias = array(
'rt' => 'rootPath',
'p' => 'realPath',
'd' => 'mkdir',
'mime' => 'getMime',
'c' => 'copy',
'r' => 'rename',
'm' => 'rename',
's' => 'saveFile',
'g' => 'getLocal',
'url' => 'getRemote_curl',
'grab' => 'getRemoteFile',
);
本类的别名为 f ,也就是 f::rt() 等效于 myFile::rootPath(),实例化时也可以简化为:$f = new f('path/to/file'),功能类和方法的别名设置,请见专题介绍。