一、常用文件函数库1、basename(); -- 返回路径中的文件名部分。string basename ( string $path [, string $suffix ] )//给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名。
参数:path 一个路径。在 windows 中,斜线(/)和反斜线(\)都可以用作目录分隔符。在其它环境下是斜线(/)
suffix 如果文件名是以 suffix 结束的,那这一部分也会被去掉。
返回值:返回 path 的基本的文件名。
$path = 'd:/test/test.txt';echo basename($path);echo
;echo basename($path,'.txt');
2、dirname(); -- 返回路径中目录部分string dirname ( string $path )//给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名。
参数:path,一个路径。在 windows 中,斜线(/)和反斜线(\)都可以用作目录分隔符。在其它环境下是斜线(/)。
返回值:返回 path 的父目录。 如果在 path 中没有斜线,则返回一个点('.'),表示当前目录。否则返回的是把path 中结尾的 /component(最后一个斜线以及后面部分)去掉之后的字符串。
$path = 'd:/test/test.txt';echo basename($path);echo
;echo basename($path,'.txt');echo
;echo dirname($path);
3、pathinfo(); --返回文件路径的信息mixed pathinfo ( string $path [, int $options = pathinfo_dirname | pathinfo_basename | pathinfo_extension | pathinfo_filename ] )//pathinfo() 返回一个关联数组包含有 path 的信息。返回关联数组还是字符串取决于 options。
参数: path 要解析的路径。
options 如果指定了,将会返回指定元素;它们包括:pathinfo_dirname,pathinfo_basename 和pathinfo_extension 或 pathinfo_filename。如果没有指定 options 默认是返回全部的单元。
返回值:如果没有传入 options ,将会返回包括以下单元的数组 array:dirname,basename 和 extension(如果有),以 及filename。
$path = 'd:/test/test.txt';var_dump(pathinfo($path));
4、filetype();--取得文件类型string filetype ( string $filename )//返回文件的类型。
参数: filename 文件的路径。
返回值: 返回文件的类型。 可能的值有 fifo,char,dir,block,link,file 和 unknown。如果出错则返回 false。如果 stat 调用失败或者文件类型未知的话 filetype() 还会产生一个 e_notice 消息。
$path = 'd:/test/test.txt';echo filetype($path);//结果file
5、fstat()和stat(); ⑴、fstat()-通过已打开的文件指针取得文件信息
array fstat ( resource $handle )//获取由文件指针 handle 所打开文件的统计信息。本函数和 stat() 函数相似,除了它是作用于已打开的文件指针而不是文件名。
参数: handle 文件系统指针,是典型地由 fopen() 创建的 resource(资源)。
返回值: 返回一个数组具有该文件的统计信息,该数组的格式详细说明于手册中 stat() 页面里。
⑵、stat() --给出文件的信息
array stat ( string $filename )//获取由 filename 指定的文件的统计信息。如果 filename 是符号连接,则统计信息是关于被连接文件本身的,而不是符号连接。//lstat() 和 stat() 相同,只除了它会返回符号连接的状态。
参数:filename 文件的路径.
$path = 'd:/test/test.txt';$fp = fopen(d:/test/test.txt,r);$fstat = fstat($fp);fclose($fp);var_dump($fstat);
6、filesize();--取得文件大小int filesize ( string $filename )//取得指定文件的大小。
参数:filename 文件的路径。
返回值:返回文件大小的字节数,如果出错返回 false 并生成一条 e_warning 级的错误。
//结果:d:/test/test.txt: 12 bytes
7、disk_free_space(); -- 返回目录中的可用空间float disk_free_space ( string $directory )//给出一个包含有一个目录的字符串,本函数将根据相应的文件系统或磁盘分区返回可用的字节数。
参数:directory 文件系统目录或者磁盘分区。
header(content-type:text/html;charset=utf8);$path = 'd:/test/test.txt';$df = disk_free_space(d:/);echo $df.字节;
8、disk_total_space(); --返回一个目录的磁盘总大小float disk_total_space ( string $directory )//给出一个包含有一个目录的字符串,本函数将根据相应的文件系统或磁盘分区返回所有的字节数。 【译者注】本函数返回的是该目录所在的磁盘分区的总大小,因此在给出同一个磁盘分区的不同目录作为参数所得到的结果完全相同。 在 unix 和 windows 200x/xp 中都支持将一个磁盘分区加载为一个子目录,这时正确使用本函数就很有意义。
参数:directory 文件系统的目录或者磁盘分区
9、fopen($filepath,$mode) resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )//fopen() 将 filename 指定的名字资源绑定到一个流上
参数:filename 如果 filename 是 scheme://... 的格式,则被当成一个 url,php 将搜索协议处理器(也被称为封装协议)来处理此模式。如果该协议尚未注册封装协议,php 将发出一条消息来帮助检查脚本中潜在的问题并将 filename 当成一个普通的文件名继续执行下去。
fopen() 中 mode 的可能值列表mode说明
'r' 只读方式打开,将文件指针指向文件头。
'r+' 读写方式打开,将文件指针指向文件头。
'w' 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
'w+' 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
'a' 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
'a+' 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
'x' 创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 false,并生成一条 e_warning 级别的错误信息。如果文件不存在则尝试创建之。这和给 底层的 open(2) 系统调用指定 o_excl|o_creat 标记是等价的。
'x+' 创建并以读写方式打开,其他的行为和 'x' 一样。
10、file();--把整个文件读入一个数组中array file ( string $filename [, int $flags = 0 [, resource $context ]] )//把整个文件读入一个数组中。
参数:filename 文件的路径。
flags 可选参数 flags 可以是以下一个或多个常量:
1、file_use_include_path 在 include_path 中查找文件。 2、file_ignore_new_lines 在数组每个元素的末尾不要添加换行符 3、file_skip_empty_lines 跳过空行。 context 一个上下文资源,创建stream_context_create()函数。
$line) { echo line #{$line_num} : . htmlspecialchars($line) .
\n;}// 另一个例子将 web 页面读入字符串。参见 file_get_contents()。$html = implode('', file('http://www.example.com/'));// 从 php 5 开始可以使用可选标记参数$trimmed = file('somefile.txt', file_ignore_new_lines | file_skip_empty_lines);?>
11、file_get_contents();-- 将整个文件读入一个字符串string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )//和 file() 一样,只除了 file_get_contents() 把文件读入一个字符串。将在参数 offset 所指定的位置开始读取长度为 maxlen 的内容。如果失败,file_get_contents() 将返回 false。
参数:filename: 要读取的文件的名称。
use_include_path:as of php 5 the file_use_include_path can be used to trigger include path search.
context:a valid context resource created with stream_context_create(). 如果你不需要自定义 context,可以用 null 来忽略。
header(content-type:text/html;charset=utf8);// php 5$file = file_get_contents('d:/test/test.txt', file_use_include_path);echo $file;//结果//this is test//this is test
12、fgets();--从文件指针中读取一行string fgets ( resource $handle [, int $length ] )//从文件指针中读取一行。
参数:handle:文件指针必须是有效的,必须指向由 fopen() 或 fsockopen() 成功打开的文件(并还未由 fclose() 关闭)。
length:从 handle 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、eof 或者已经读取了 length - 1 字节后停止(看先碰到那一种情况)。如果没有指定length,则默认为 1k,或者说 1024 字节。
13、ftell();-- 返回文件指针读/写的位置
int ftell ( resource $handle )//返回由 handle 指定的文件指针的位置,也就是文件流中的偏移量。
参数:handle : 文件指针必须是有效的,且必须指向一个通过 fopen() 或 popen() 成功打开的文件。在附加模式(加参数 a 打开文件)中 ftell() 会返回未定义错误。
header(content-type:text/html;charset=utf8);// opens a file and read some data$fp = fopen(d:/test/test.txt, r);$data = fgets($fp, 4);// where are we ?echo ftell($fp); // 结果3fclose($fp);
14、fseek();--在文件指针中定位int fseek ( resource $handle , int $offset [, int $whence = seek_set ] )//在与 handle 关联的文件中设定文件指针位置。 新位置从文件头开始以字节数度量,是以 whence 指定的位置加上 offset。
参数 :handle:文件系统指针,是典型地由 fopen() 创建的 resource(资源)。
offset:偏移量。要移动到文件尾之前的位置,需要给 offset 传递一个负值,并设置 whence 为 seek_end。
whence values are: 1、seek_set - 设定位置等于 offset 字节。2、seek_cur - 设定位置为当前位置加上 offset。2、seek_end - 设定位置为文件尾加上 offset。
header(content-type:text/html;charset=utf8);$fp = fopen('d:\test\test.txt', 'r');// read some data$data = fgets($fp, 4096);// move back to the beginning of the file// same as rewind($fp); fseek($fp, 0);
15、flock();--轻便的咨询文件锁定 bool flock ( resource $handle , int $operation [, int &$wouldblock ] )//flock() 允许执行一个简单的可以在任何平台中使用的读取/写入模型(包括大部分的 unix 派生版和甚至是 windows)。
参数:handle 文件系统指针,是典型地由 fopen() 创建的 resource(资源)。
operation 可以是以下值之一:1、lock_sh取得共享锁定(读取的程序)。2、lock_ex 取得独占锁定(写入的程序。3、lock_un 释放锁定(无论共享或独占)。
如果不希望 flock() 在锁定时堵塞,则是 lock_nb(windows 上还不支持)。
wouldblock:如果锁定会堵塞的话(ewouldblock 错误码情况下),可选的第三个参数会被设置为 true。(windows 上不支持)
if (flock($fp, lock_ex)) { // 进行排它型锁定 ftruncate($fp, 0); // truncate file fwrite($fp, write something here\n); fflush($fp); // flush output before releasing the lock flock($fp, lock_un); // 释放锁定} else { echo couldn't get the lock!;}fclose($fp);
16、is_readable --判断给定文件名是否可读bool is_readable ( string $filename )//判断给定文件名是否存在并且可读。
参数:filename:文件的路径。
返回值:如果由 filename 指定的文件或目录存在并且可读则返回 true,否则返回 false。
$filename = 'd:\test\test.txt';if (is_readable($filename)) { echo 'the file is readable';} else { echo 'the file is not readable';}//the file is readable
17、is_writeable -- 判断给定的文件名是否可写bool is_writable ( string $filename )//如果文件存在并且可写则返回 true。filename 参数可以是一个允许进行是否可写检查的目录名。
参数:filename 要检查的文件名称。
$filename = 'd:\test\test.txt';if (is_writeable($filename)) { echo 'the file is writeable';} else { echo 'the file is not writeable';}//the file is writeable
18、chown(); -- 改变文件的所有者bool chown ( string $filename , mixed $user )//尝试将文件 filename 的所有者改成用户 user(由用户名或用户 id 指定)。 只有超级用户可以改变文件的所有者。
参数:filename:文件路径。
user:用户名或数字。
二、目录函数1、is_dir();--判断给定文件名是否是一个目录bool is_dir ( string $filename )//判断给定文件名是否是一个目录。
参数:filename:如果文件名存在并且为目录则返回 true。如果 filename 是一个相对路径,则按照当前工作目录检查其相对路径。
$filename = 'd:\test\test.txt';var_dump(is_dir('$filename')); //bool(false) var_dump(is_dir('d:\test')); //bool(true)
2、mkdir();--新建目录bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
//尝试新建一个由 pathname 指定的目录。
参数:pathname:目录的路径。
mode:默认的 mode 是 0777,意味着最大可能的访问权。有关 mode 的更多信息请阅读 chmod() 页面。
mkdir(d:/test/test1, 0700);
3、opendir();--打开目录句柄resource opendir ( string $path [, resource $context ] )//打开一个目录句柄,可用于之后的 closedir(),readdir() 和 rewinddir() 调用中。
参数:path 要打开的目录路径
context 参数的说明见手册中的 streams api 一章。
4、readdir();--从目录句柄中读取条目string readdir ([ resource $dir_handle ] )//返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。
参数:dir_handle 目录句柄的 resource,之前由 opendir() 打开
header(content-type:text/html;charset=utf8);if ($handle = opendir('d:/test')) { echo directory handle: $handle\n; echo files:\n; /* 这是正确地遍历目录方法 */ while (false !== ($file = readdir($handle))) { echo $file\n; } /* 这是错误地遍历目录的方法 while ($file = readdir($handle)) { echo $file\n; } */ closedir($handle);}