使用fwrite函数和file_put_contents函数
<?php
$filepath="1.txt";
$str="此情可待成追忆 只是当时已惘然<br>";
echo "用fwrite函数写入文件";
$fp=fopen($filepath,'rb') or die('文件不存在');
fwrite($fp,$str);
fclose($fp);
readfile($filepath);
echo "<p>用file_put_contents函数写入文件";
file_put_contents($filepath,$str);
readfile($filepath);
?>