PHP读写文件的方法

[ 2008-08-24 12:22:24 | Author: colacat ]
Font Size: Large | Medium | Small
以下代码向filename.txt文件中写入指定内容并输出:
<?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;
?>

上面的代码是向文件的末尾写入指定内容,如果想直接取代原有内容,则将fopen("filename.txt","a+")改为fopen("filename.txt","w+"),fopen()函数的详细说明:http://www.zzsky.cn/build/content/1186.htm,如果想往文件头部写入内容且保留原有内容,则只能先读取原有内容,然后加上要写入的内容,再新建文件,最后写入。
Comments Feed Comments Feed: http://www.colacat.cn/feed.asp?q=comment&id=90

There is no comment on this article.

Post Comment
Smilies
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
Enable UBB Codes
Auto Convert URL
Show Smilies
Hidden Comment
Username:   Password:   Register Now?
Security Code * Please Enter the Security Code