How do I write to a file in php?

2019-09-11 12:54发布

问题:

I am getting the source of a page as follows:

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

and printing it on the screen but to be honest I actually want to replace an existing html file with it every 1 min. (I will be opening the file every 1 min.)

How could I do this?

回答1:

If you've learned to use file_get_contents() then consider his sister function file_put_contents()

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

To run it every minute, look at something like cron (or its equivalent on your operating platform of choice)