调用: array stat (string $filename 输出: 返回由 filename 指定的文件的统计信息
文件操作
127.fwrite(): 写入文件
$filename = 'test.txt'; $somecontent = "添加这些文字到文件n"; $handle = fopen($filename, 'a'); fwrite($handle, $somecontent); fclose($handle);
调用: int fwrite ( resource handle, string string [, int length] )
输出: 把 string 的内容写入 文件指针 handle 处。如果指定了 length,当写入了length个字节或者写完了string以后,写入就会停止, 视乎先碰到哪种情况
128.fputs(): 同上
129.fread(): 读取文件
$filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle);
调用: string fread ( int handle, int length ) 从文件指针handle,读取最多 length 个字节
130.feof(): 检测文件指针是否到了文件结束的位置
$file = @fopen("no_such_file", "r");
while (!feof($file)) {
}
fclose($file);
调用: bool feof ( resource handle ) 输出: 如果文件指针到了 EOF 或者出错时则返回TRUE,否则返回一个错误(包括 socket 超时),其它情况则返回 FALSE
131.fgets(): 从文件指针中读取一行
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
调用: string fgets ( int handle [, int length] ) 输出: 从handle指向的文件中读取一行并返回长度最多为length-1字节的字符串.碰到换行符(包括在返回值中)、EOF 或者已经读取了length -1字节后停止(看先碰到那一种情况). 如果没有指定 length,则默认为1K, 或者说 1024 字节.
132.fgetc(): 从文件指针中读取字符
$fp = fopen('somefile.txt', 'r');
if (!$fp) {
echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp))) {
echo "$charn";
}
输入: string fgetc ( resource $handle ) 输出: 返回一个包含有一个字符的字符串,该字符从 handle指向的文件中得到. 碰到 EOF 则返回 FALSE.
133.file(): 把整个文件读入一个数组中
$lines = file('http://www.example.com/');
// 在数组中循环,显示 HTML 的源文件并加上行号。
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " .
htmlspecialchars($line) . "<br />n";
}
// 另一个例子将 web 页面读入字符串。参见 file_get_contents()。
$html = implode('', file('http://www.example.com/'));
调用: array file ( string $filename [, int $use_include_path [, resource $context ]] )
输出: 数组中的每个单元都是文件中相应的一行,包括换行符在内。如果失败 file() 返回 FALSE
134.readfile(): 输出一个文件 调用: int readfile ( string $filename [, bool $use_include_path [, resource $context ]] )







