文件操作类

属性
命名空间 fize\io
类名 File
方法:
方法名 说明
__construct() 构造
__destruct() 析构
getSplFileObject() 返回对应的SPL文件对象
getStream() 获取资源流
basename() 返回路径中的文件名部分
chgrp() 改变当前文件所属的组
chmod() 改变当前文件模式
chown() 改变当前文件的所有者
clearstatcache() 清除当前文件状态缓存
copy() 将当前文件拷贝到路径dest
delete() 删除当前文件
dirname() 返回当前文件路径中的目录部分
close() 关闭当前文件
eof() 测试文件指针是否到了文件结束的位置
flush() 将缓冲内容输出到文件
getc() 从文件指针中读取一个字符。 碰到 EOF 则返回 FALSE 。
getcsv() 从文件指针中读入一行并解析 CSV 字段
gets() 从文件指针中读取一行
getss() 从文件指针中读取一行并过滤掉HTML和PHP标记。
exists() 检查文件是否存在
getContents() 将整个文件读入一个字符串
putContents() 将一个字符串写入文件
file() 把整个文件读入一个数组中
atime() 取得文件的上次访问时间
ctime() 取得文件的 inode 修改时间
group() 取得文件的组
inode() 文件的inode
mtime() 文件修改时间
owner() 文件的所有者
perms() 文件的权限
size() 文件大小(字节数)
type() 文件类型
lock() 轻便的咨询文件锁定
nmatch() 检查是否模式匹配文件名
open() 打开文件或者 URL
passthru() 输出文件指针处的所有剩余数据
putcsv() 将行格式化为 CSV 并写入文件指针
puts() 写入文件(可安全用于二进制文件)
read() 读取文件(可安全用于二进制文件)
scanf() 从文件中格式化输入
seek() 在文件指针中定位
stat() 通过已打开的文件指针取得文件信息
tell() 返回文件指针读/写的位置
truncate() 将文件截断到给定的长度
write() 写入文件(可安全用于二进制文件)
isExecutable() 文件是否可执行
isFile() 判断是否为一个正常的文件
isLink() 判断是否为符号连接
isReadable() 判断是否可读
isUploadedFile() 判断当前文件是否是通过 HTTP POST 上传的
isWritable() 判断当前文件是否可写
link() 建立一个硬连接
linkinfo() 获取一个连接的信息
pathinfo() 返回文件路径的信息
popen() 打开一个指向进程的管道
readfile() 读取文件并写入到输出缓冲。
readlink() 返回符号连接指向的目标
realpathCacheGet() 获取真实目录缓存的详情
realpathCacheSize() 获取真实路径缓冲区的大小
realpath() 返回规范化的绝对路径名
rename() 重命名一个文件,可用于移动文件
rewind() 倒回文件指针的位置
setBuffer() 设置当前打开文件的缓冲大小。
symlink() 建立一个名为 link 的符号连接。
tmpfile() 建立一个临时文件
touch() 设定文件的访问和修改时间
umask() 获取或改变当前的umask
unlink() 删除文件

方法

__construct()

构造

public function __construct (

    string $filename = null,

    string $mode = null

)
参数:
名称 说明
filename 文件名
mode 打开模式
参数 `$filename` :
对于 popen 可以使用null来指定
可以传入上下文流进行流操作

__destruct()

析构

public function __destruct ()
清理资源对象,防止内存泄漏

getSplFileObject()

返回对应的SPL文件对象

public function getSplFileObject () : \SplFileObject

getStream()

获取资源流

public function getStream () : resource

basename()

返回路径中的文件名部分

public function basename (

    string $suffix = null

) : string
参数:
名称 说明
suffix 如果文件名是以 suffix 结束的,那这一部分也会被去掉

chgrp()

改变当前文件所属的组

public function chgrp (

    mixed $group

) : bool
参数:
名称 说明
group 组的名称或数字。
该函数不能在 Windows 系统上运行
只有超级用户可以任意修改文件的组,其它用户可能只能将文件的组改成该用户自己所在的组。

chmod()

改变当前文件模式

public function chmod (

    int $mode

) : bool
参数:
名称 说明
mode 模式
参数 `$mode` :
注意 mode 不会被自动当成八进制数值,而且也不能用字符串(例如 "g+w")。
要确保正确操作,需要给 mode 前面加上 0

chown()

改变当前文件的所有者

public function chown (

    mixed $user

) : bool
参数:
名称 说明
user 用户名或数字。
该函数不能在 Windows 系统上运行

clearstatcache()

清除当前文件状态缓存

public function clearstatcache ()

copy()

将当前文件拷贝到路径dest

public function copy (

    string $dir,

    string $name = null,

    bool $cover = false

) : bool
参数:
名称 说明
dir 指定要复制的文件夹路径
name 指定文件名,不指定则为原文件名
cover 如果指定文件存在,是否覆盖

delete()

删除当前文件

public function delete (

    resource $context = null

) : bool
参数:
名称 说明
context 上下文
返回值:

没有该文件也返回true

dirname()

返回当前文件路径中的目录部分

public function dirname () : string

close()

关闭当前文件

public function close () : bool

eof()

测试文件指针是否到了文件结束的位置

public function eof () : bool

flush()

将缓冲内容输出到文件

public function flush () : bool

getc()

从文件指针中读取一个字符。 碰到 EOF 则返回 FALSE 。

public function getc () : string
返回值:如果碰到 EOF 则返回 FALSE。

getcsv()

从文件指针中读入一行并解析 CSV 字段

public function getcsv (

    int $length = 0,

    string $delimiter = ",",

    string $enclosure = "\"",

    string $escape = "\\"

) : array
参数:
名称 说明
length 规定行的最大长度
delimiter 设置字段分界符
enclosure 设置字段环绕符
escape 设置转义字符
返回值:

如果碰到 EOF 则返回 FALSE。

参数 `$length` :
  必须大于 CVS 文件内最长的一行。
参数 `$delimiter` :
  (只允许一个字符),默认值为逗号。
参数 `$enclosure` :
  (只允许一个字符),默认值为双引号。
参数 `$escape` :
  (只允许一个字符),默认是一个反斜杠。

gets()

从文件指针中读取一行

public function gets (

    int $length = null

) : string
参数:
名称 说明
length 规定要读取的字节数
返回值:

若失败,则返回 false。

参数 `$length` :
默认是 1024 字节。
实际返回的字节是 $length - 1

getss()

从文件指针中读取一行并过滤掉HTML和PHP标记。

public function getss (

    int $length = null,

    string $allowable_tags = null

) : string
参数:
名称 说明
length 规定要读取的字节数
allowable_tags 规定不会被删除的标签
参数 `$length` :
  默认是 1024 字节
参数 `$allowable_tags` :
  形如“<p>,<b>

exists()

检查文件是否存在

public static function exists (

    string $path

) : bool
参数:
名称 说明
path 路径

getContents()

将整个文件读入一个字符串

public function getContents (

    bool $use_include_path = false,

    resource $context = null,

    int $offset = 0,

    int $maxlen = null

) : string
参数:
名称 说明
use_include_path 是否在 include_path 中搜寻文件
context 上下文支持
offset 插入位置偏移量
maxlen 指定读取长度
参数 `$offset` :
  默认为0表示最开始地方
参数 `$maxlen` :
  超过该长度则不读取,默认不指定全部读取

putContents()

将一个字符串写入文件

public function putContents (

    mixed $data,

    int $flags = 0,

    resource $context = null

) : int
参数:
名称 说明
data 要写入的数据
flags 指定配置
context 上下文支持
参数 `$data` :
  类型可以是 string  array 或者是 stream 资源
参数  `$flags` :
  可选值:[FILE_USE_INCLUDE_PATH|FILE_APPEND|LOCK_EX]

file()

把整个文件读入一个数组中

public function file (

    int $flags = 0

) : array
参数:
名称 说明
flags 指定配置
参数 `$flags` :
可选值:[FILE_USE_INCLUDE_PATH|FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES]

atime()

取得文件的上次访问时间

public function atime () : int

ctime()

取得文件的 inode 修改时间

public function ctime () : int

group()

取得文件的组

public function group () : int

inode()

文件的inode

public function inode () : int

mtime()

文件修改时间

public function mtime () : int

owner()

文件的所有者

public function owner () : int

perms()

文件的权限

public function perms () : int

size()

文件大小(字节数)

public function size () : int

type()

文件类型

public function type () : string
(可能的值有 fifo,char,dir,block,link,file  unknown。)

lock()

轻便的咨询文件锁定

public function lock (

    int $operation,

    int &$wouldblock = null

) : bool
参数:
名称 说明
operation 操作
wouldblock 如果锁定会堵塞的话返回1
参数 `$operation` :
可选值:[LOCK_SH|LOCK_EX|LOCK_UN]

nmatch()

检查是否模式匹配文件名

public function nmatch (

    string $pattern,

    int $flags = 0

) : bool
参数:
名称 说明
pattern 统配符[shell]
flags 指定配置
普通用户可能习惯于 shell 模式或者至少其中最简单的形式 '?'  '*' 通配符,
因此使用 fnmatch() 来代替 Preg::match() 来进行前端搜索表达式输入对于非程序员用户更加方便。
参数 `$flags` :
  可选值:[FNM_NOESCAPE|FNM_PATHNAME|FNM_PERIOD|FNM_CASEFOLD]

open()

打开文件或者 URL

public function open (

    mixed $mode = null,

    bool $use_include_path = false,

    resource $context = null

)
参数:
名称 说明
mode  
use_include_path 是否在 include_path 中搜寻文件
context 上下文支持

passthru()

输出文件指针处的所有剩余数据

public function passthru () : int
返回值:返回剩余数据字节数

putcsv()

将行格式化为 CSV 并写入文件指针

public function putcsv (

    array $fields,

    string $delimiter = ",",

    string $enclosure = "\"",

    string $escape_char = "\\"

) : int
参数:
名称 说明
fields 要写入的数组数据
delimiter 分隔符
enclosure 界限符
escape_char 转义符
返回值:

如果失败返回false

puts()

写入文件(可安全用于二进制文件)

public function puts (

    string $string,

    int $length = null

) : int
参数:
名称 说明
string 要写入的字符串
length 指定写入长度
返回值:

如果失败返回false

read()

读取文件(可安全用于二进制文件)

public function read (

    int $length

) : string
参数:
名称 说明
length  

scanf()

从文件中格式化输入

public function scanf (

    string $format

) : array
参数:
名称 说明
format  

seek()

在文件指针中定位

public function seek (

    int $offset,

    int $whence = 0

) : int
参数:
名称 说明
offset 偏移量
whence 设置方式
参数 `$offset` :
  要移动到文件尾之前的位置,需要给 offset 传递一个负值,并设置 whence  SEEK_END。
参数 `$whence` :
  SEEK_SET - 设定位置等于 offset 字节。
  SEEK_CUR - 设定位置为当前位置加上 offset。
  SEEK_END - 设定位置为文件尾加上 offset。

stat()

通过已打开的文件指针取得文件信息

public function stat () : array

tell()

返回文件指针读/写的位置

public function tell () : int

truncate()

将文件截断到给定的长度

public function truncate (

    int $size

) : bool
参数:
名称 说明
size 指定长度

write()

写入文件(可安全用于二进制文件)

public function write (

    string $string,

    int $length = null

) : int
参数:
名称 说明
string 要写入的字符串
length 指定写入长度
返回值:

失败时返回false

isExecutable()

文件是否可执行

public function isExecutable () : bool

isFile()

判断是否为一个正常的文件

public function isFile () : bool

isReadable()

判断是否可读

public function isReadable () : bool

isUploadedFile()

判断当前文件是否是通过 HTTP POST 上传的

public function isUploadedFile () : bool

isWritable()

判断当前文件是否可写

public function isWritable () : bool

linkinfo()

获取一个连接的信息

public function linkinfo () : int

pathinfo()

返回文件路径的信息

public function pathinfo (

    mixed $options = null

) : mixed
参数:
名称 说明
options 选项
参数 `$options` :
如果没有传入 options ,将会返回包括以下单元的数组 array :dirname,basename和 extension(如果有),以 及filename。

popen()

打开一个指向进程的管道

public function popen (

    string $command,

    mixed $mode = null

)
参数:
名称 说明
command 命令
mode  

readfile()

读取文件并写入到输出缓冲。

public function readfile (

    bool $use_include_path = false,

    resource $context = null

) : int
参数:
名称 说明
use_include_path 是否在 include_path 中搜寻文件
context 上下文支持

realpathCacheGet()

获取真实目录缓存的详情

public static function realpathCacheGet () : array

realpathCacheSize()

获取真实路径缓冲区的大小

public static function realpathCacheSize () : int

realpath()

返回规范化的绝对路径名

public function realpath () : string

rename()

重命名一个文件,可用于移动文件

public function rename (

    string $newname,

    bool $auto_build = true

) : bool
参数:
名称 说明
newname 要移动到的目标位置路径
auto_build 如果指定的路径不存在,是否创建

rewind()

倒回文件指针的位置

public function rewind () : bool

setBuffer()

设置当前打开文件的缓冲大小。

public function setBuffer (

    int $buffer

) : mixed
参数:
名称 说明
buffer 规定缓冲大小,以字节计。
返回值:

未启动句柄时返回false;否则如果成功,该函数返回 0,否则返回 EOF。

tmpfile()

建立一个临时文件

public static function tmpfile () : resource

touch()

设定文件的访问和修改时间

public function touch (

    int $time = null,

    int $atime = null

) : bool
参数:
名称 说明
time 要设定的修改时间
atime 要设定的访问时间
注意,如果文件不存在则尝试创建

umask()

获取或改变当前的umask

public static function umask (

    int $mask = null

) : int
参数:
名称 说明
mask 指定该值时将改变当前umask