fopen() 函数用于在 PHP 中打开文件。
打开文件
fopen() 函数用于在 PHP 中打开文件。
此函数的第一个参数含有要打开的文件的名称,第二个参数规定了使用哪种模式来打开文件:
<html>
<body>
<?php
$file=fopen("welcome.txt","r");
?>
</body>
</html>
文件可能通过下列模式来打开:
| 模式 | 描述 |
|---|---|
| r | 只读。在文件的开头开始。 |
| r+ | 读/写。在文件的开头开始。 |
| w | 只写。打开并清空文件的内容;如果文件不存在,则创建新文件。 |
| w+ | 读/写。打开并清空文件的内容;如果文件不存在,则创建新文件。 |
| a | 追加。打开并向文件文件的末端进行写操作,如果文件不存在,则创建新文件。 |
| a+ | 读/追加。通过向文件末端写内容,来保持文件内容。 |
| x | 只写。创建新文件。如果文件以存在,则返回 FALSE。 |
| x+ |
读/写。创建新文件。如果文件已存在,则返回 FALSE 和一个错误。 注释:如果 fopen() 无法打开指定文件,则返回 0 (false)。 |
例子
如果 fopen() 不能打开指定的文件,下面的例子会生成一段消息:
<html>
<body>
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
?>
</body>
</html>
关闭文件
fclose() 函数用于关闭打开的文件。
<?php
$file = fopen("test.txt","r");
//some code to be executed
fclose($file);
?>
检测 End-of-file
feof() 函数检测是否已达到文件的末端 (EOF)。
在循环遍历未知长度的数据时,feof() 函数很有用。
注释: 在 w 、a 以及 x 模式,您无法读取打开的文件!
if (feof($file)) echo "End of file";
逐行读取文件
fgets() 函数用于从文件中逐行读取文件。
注释: 在调用该函数之后,文件指针会移动到下一行。
例子
下面的例子逐行读取文件,直到文件末端为止:
<?php
$file = fopen("welcome.txt", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
逐字符读取文件
fgetc() 函数用于从文件逐字符地读取文件。
注释: 在调用该函数之后,文件指针会移动到下一个字符。
例子
下面的例子逐字符地读取文件,直到文件末端为止:
<?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");
while (!feof($file))
{
echo fgetc($file);
}
fclose($file);
?>
<td align="right">您是本站第
<?php
$countfile = "count.txt";
/*检查是否存在count.txt这个文件,如果不存在就创建一个*/
if(!file_exists($countfile))
{
exec("echo 0>$countfile");
}
/*如果有新的IP访问网站,那么计数器加1,然后删除现有的count.txt文件,重新建立一个并写入写的计数器值*/
function displaycount($countfile)
{
include("conn.php");
$fp = fopen($countfile, "rw");
$sum = fgets($fp, 5);
$ip = getenv("REMOTE_ADDR");
$sql = mssql_query("select * from s_ip where ip='".$ip."'", $conn);
$info = mssql_fetch_array($sql);
if($info==false)
{
mssql_query("insert into s_ip(ip) values ('$ip')", $conn);
$sum += 1;
}
echo $sum;
exec("rm -rf $countfile");
exec("echo $sum>$countfile");
}
displaycount($countfile);
?>
位访客
</td>
3012

被折叠的 条评论
为什么被折叠?



