如何在php中写入文件?

问题描述:

我得到一个页面的源代码如下:如何在php中写入文件?

<? $txt = file_get_contents('http://stats.pingdom.com/file'); 
echo $txt; ?> 

并打印在屏幕上,但说实话其实我是想取代其现有的HTML文件中的每个1分钟。 (我将每1分钟打开一次文件)

我该怎么做?

+0

为什么你甚至需要一个PHP脚本来做到这一点?您是否尝试过使用'wget http://stats.pingdom.com/file -o/path/to/the/file/locally',并将其添加到您的crontab */1 * * * *'中以便每分钟运行一次? – 2012-03-17 19:24:00

如果你已经学会了使用的file_get_contents()再考虑他的妹妹功能file_put_contents()

$txt = file_get_contents('http://stats.pingdom.com/file'); 
file_put_contents('/path/to/my/file.html',$txt); 

要运行它的每一分钟,看东西比如cron(或等值您选择的操作平台上)

+0

谢谢,这需要一个绝对路径? – Chriswede 2012-03-17 19:47:42

+0

,因为我得到这个:Warning:file_put_contents(/home/xyz/public_html/stats/stats.html)[function.file-put-contents]:未能打开流:权限在/ home/xyz/public_html/index中被拒绝第3行的.php – Chriswede 2012-03-17 19:50:49

+0

文件夹统计信息是777 – Chriswede 2012-03-17 19:53:16