第十二招 PHP之文件处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lwqBrell/article/details/85614690

目录操作

打开和关闭目录

resource opendir ( string $path [, resource $context ] )

path
要打开的目录路径

context
context 参数的说明见手册中的 Streams API 一章。

string readdir ([ resource $dir_handle ] )

返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。

void closedir ([ resource $dir_handle ] )

dir_handle
目录句柄的 resource,之前由 opendir() 所打开的。如果目录句柄没有指定,那么会假定为是opendir()所打

开的最后一个句柄。

创建和删除目录

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context]]] )

pathname
目录的路径。

mode
默认的 mode 是 0777,意味着最大可能的访问权。有关 mode 的更多信息请阅读 chmod() 页面。

Note:

mode 在 Windows 下被忽略。

注意也许想用八进制数指定模式,也就是说该数应以零打头。模式也会被当前的 umask 修改,可以用 umask() 来改变。

recursive
Allows the creation of nested directories specified in the pathname.

context
Note: 在 PHP 5.0.0 中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见 Streams。

bool rmdir ( string $dirname [, resource $context ] )

dirname
目录的路径。

context
Note: 在 PHP 5.0.0 中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见 Streams。
 

判断文件类型

string filetype ( string $filename )

filename
文件的路径。

文件操作

打开文件

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

filename
如果 filename 是 "scheme://..." 的格式,则被当成一个 URL,PHP 将搜索协议处理器(也被称为封装协议)来处理此模式。如果该协议尚未注册封装协议,PHP 将发出一条消息来帮助检查脚本中潜在的问题并将 filename 当成一个普通的文件名继续执行下去。

如果 PHP 认为 filename 指定的是一个本地文件,将尝试在该文件上打开一个流。该文件必须是 PHP 可以访问的,因此需要确认文件访问权限允许该访问。如果激活了安全模式或者 open_basedir 则会应用进一步的限制。

如果 PHP 认为 filename 指定的是一个已注册的协议,而该协议被注册为一个网络 URL,PHP 将检查并确认 allow_url_fopen 已被激活。如果关闭了,PHP 将发出一个警告,而 fopen 的调用则失败。

读取文件

string fgets ( resource $handle [, int $length ] )

从文件指针中读取一行。

handle
文件指针必须是有效的,必须指向由 fopen() 或 fsockopen() 成功打开的文件(并还未由 fclose() 关闭)。

length
从 handle 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值

中)、EOF 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定 length,则默认

为 1K,或者说 1024 字节。

string fgetc ( resource $handle )

从文件句柄中获取一个字符。

handle
文件指针必须是有效的,必须指向由 fopen() 或 fsockopen() 成功打开的文件(并还未由 fclose() 关闭)。

文件的简易操作

bool copy ( string $source , string $dest [, resource $context ] )

source
源文件路径。

dest
目标路径。如果 dest 是一个 URL,则如果封装协议不支持覆盖已有的文件时拷贝操作会失败。

Warning
如果目标文件已存在,将会被覆盖。

context
A valid context resource created with stream_context_create().

bool rename ( string $oldname , string $newname [, resource $context ] )

oldname
Note:

用于 oldname 中的封装协议必须和用于 newname 中的相匹配。

newname
新的名字。

context
Note: 在 PHP 5.0.0 中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见 Streams。

bool unlink ( string $filename [, resource $context ] )

filename
文件的路径。

context
Note: 在 PHP 5.0.0 中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见 Streams。

猜你喜欢

转载自blog.csdn.net/lwqBrell/article/details/85614690
今日推荐