文件的打开和关闭

主要是两个函数,fopen和fclose。

fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] ) : resource

返回的是资源类型的数据
mode 打开模式:指的是文件打开是以写入、读取、执行等等方式

r:(只读模式)以只读方式打开,文件指针指向文件的开头部分
r+:(读写模式)以读写方式打开,文件指针指向文件的开头

w:(只写模式)以写方式打开,指针指向文件头部,如果文件不存在,会创建一个新文件,如果文件存在,文件内容会被清空
w+:(写读模式),指针指向文件头部,如果文件不存在,会创建一个新文件,如果文件存在,文件内容会被清空,可以读取

a:以追加方式打开,指针指向文件末尾(只写),如果文件不存在,会创建一个新文件
a+:以追加方式打开,如果文件存在,指针指向文件末尾,(读写)如果文件不存在,会创建一个新文件

文件的读取

可以读取单个字符、一行、整个文件、读任意长度的数据。

1、读取整个文件:

不需要对文件打开与关闭

  • readfile()
readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] ) : int

读入一个文件并把它输出(output)到缓存中(buffer) 返回读取文件的字节数,错误则返回false。

  • file()
file ( string $filename [, int $flags = 0 [, resource $context ]] ) : array

读取整个文件内容到一个数组,数组每个元素对应一行。

  • file_get_contents()
file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = 0 [, int $maxlen ]]]] ) : string
将文件读入到一个字符串中,从 offset 开始,读取 maxlen 长度。
该函数适用于二进制对象,是将整个文件内容读取到一个字符串中的首选方式

2、读取一行:

  • fgets() 读取一行数据
fgets ( resource $handle [, int $length ] ) : string

在读取指定长度 length 或碰到新行或碰到 eof 时结束。

  • fgetss()
fgetss ( resource $handle [, int $length [, string $allowable_tags ]] ) : string

是fgets()的变体,用于读取一行数据,不过 会过滤掉被读取内容的html和php标记,效果同strip_tags()

php 7.3.0 中,已被废弃,不推荐使用。

3、读取单个字符:

fgetc()

fgetc ( resource $handle ) : string

在对某一个字符进行查找、替换时,需要有针对性的对某个字符进行读取,那么我们就可以使用fgetc来实现

4、读取任意长度的字符:

fread()

fread ( resource $handle , int $length ) : string

读取汉字时,根据汉字的字节数避免出现乱码

文件的写入

  • fwrite()
  • fputs()
  • file_put_contents(filename,$str)
    不需要打开关闭
    相当于 fopen(“”,”w\w+”)->fwrite()->fclose()
    第三个参数(file_append)可以实现追加功能

文件的其他操作

  • 复制 copy
copy ( string $source , string $dest [, resource $context ] ) : bool
  • 重命名 rename
    文件或文件夹,可实现剪切功能
rename ( string $oldname , string $newname [, resource $context ] ) : bool
  • 删除文件 unlink
unlink ( string $filename [, resource $context ] ) : bool
  • 获取文件的大小 filesize
filesize ( string $filename ) : int
  • 获取文件的绝对路径 realpath
realpath ( string $path ) : string

返回文件的绝对路径(盘符绝对路径)

  • 获取文件的相关信息 stat
    stat(“filename”)
    返回一个数组,数组元素是文件的相关信息,包括文件大小、最后的修改时间等
  • 判断是否可写 is_writeable
  • 判断是否可读 is_readable
  • 判断是否是个文件 is_file
  • 判断某个文件或者目录是否存在 file_exists
  • 文件最后一次被访问的时间 fileatime
    last access
  • 文件最后一次修改的时间 filemtime
    modification

文件指针的操作

php可以实现文件指针的定位和查询,从而实现所需信息的查询,文件指针有四个函数
rewind feof ftell fseek

  • rewind(资源类型):重置指针的位置
  • ftell(资源类型):返回文件读写指针所在的位置,整型值
  • feof(资源类型):该函数判断文件指针是否指向文件末尾
  • fseek() 位移 距离
fseek ( resource $handle , int $offset [, int $whence = seek_set ] ) : int

whence 可能的值
seek_set – set position equal to offset bytes.
seek_cur – set position to current location plus offset.
seek_end – set position to end-of-file plus offset.

文件锁 flock

在向一个文件写入内容时,需要先将文件锁定,以防止其他用户同时修改该文件。

flock ( resource $handle , int $operation [, int &$wouldblock ] ) : bool

operation
lock_sh: 取得共享锁
lock_ex: 取得独占锁 exclusive 排外
lock_un: 释放锁定
lock_nb: 防止flock()在锁定的时候堵塞 (防止多人同时锁定一个文件)

远程文件读取

在php中支持url格式的文件的调用
在php.ini中开启 allow_url_fopen = on

//readfile(文件地址):将文件内容读取到内存中

数据流(输出流):内存输出到屏幕的数据流
ob_start(): 开启输出流

ob_get_contents():得到输出流中缓冲的内容

ob_clean:关闭输出流
可以用来读取网络上的图片