PHP读写文件的方法
[ 2008-08-24 12:22:24 | Author: colacat ]
以下代码向filename.txt文件中写入指定内容并输出:
上面的代码是向文件的末尾写入指定内容,如果想直接取代原有内容,则将fopen("filename.txt","a+")改为fopen("filename.txt","w+"),fopen()函数的详细说明:http://www.zzsky.cn/build/content/1186.htm,如果想往文件头部写入内容且保留原有内容,则只能先读取原有内容,然后加上要写入的内容,再新建文件,最后写入。
Comments Feed: http://www.colacat.cn/feed.asp?q=comment&id=90
<?php
//写入内容
$input="写入文件的内容";
$fp=fopen("filename.txt","a+");
fwrite($fp,$input);
fclose($fp);
//读取内容
$fp=fopen("filename.txt","r");
$output=fread($fp,filesize("filename.txt"));
fclose($fp);
//输出内容
echo $output;
?>
//写入内容
$input="写入文件的内容";
$fp=fopen("filename.txt","a+");
fwrite($fp,$input);
fclose($fp);
//读取内容
$fp=fopen("filename.txt","r");
$output=fread($fp,filesize("filename.txt"));
fclose($fp);
//输出内容
echo $output;
?>
上面的代码是向文件的末尾写入指定内容,如果想直接取代原有内容,则将fopen("filename.txt","a+")改为fopen("filename.txt","w+"),fopen()函数的详细说明:http://www.zzsky.cn/build/content/1186.htm,如果想往文件头部写入内容且保留原有内容,则只能先读取原有内容,然后加上要写入的内容,再新建文件,最后写入。
Comments Feed: http://www.colacat.cn/feed.asp?q=comment&id=90
There is no comment on this article.






