Warning: file_put_contents - cannot create file using php

Chriswede picture Chriswede · Mar 17, 2012 · Viewed 7.5k times · Source

When I am trying to do this:

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

I am getting following error:

Warning: file_put_contents(stats/stats.html) [function.file-put-contents]: failed to open stream: Permission denied in /home/jtf/public_html/index.php on line 2

the folder stats is 777

what am I doing wrong?

thanks

Answer

Jon picture Jon · Mar 17, 2012

/stats and stats (or the equivalent, ./stats) are not necessarily the same directory (in fact they might refer to the same directory only in theory). Most likely you are currently trying to write to a directory that does not even exist.

So this should work:

file_put_contents('stats/file.html',$txt); // removed "/" prefix