-1

I have the following code:

$cachefile = "http://www.DOMAIN.com/users/{$user}.php"; 
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing 
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file 
fclose($fp); // close the file 
ob_end_clean(); // Send the output to the browser

chmod($cachefile, 0644);

The file path is not valid, as it produces this error: Warning: chmod() [function.chmod]: No such file or directory in ...

However, this code does:

$cachefile = "{$user}.php"; 
$fp = fopen($cachefile, 'w'); // open the cache file "cache/home.html" for writing 
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file 
fclose($fp); // close the file 
ob_end_clean(); // Send the output to the browser

chmod($cachefile, 0644);

The file--using the second block of code--is created in the same folder as the script that created it. However, I would like to save it to the first block's location. I look on php.net for fopen and I didn't see that I was doing anything wrong, but obviously I am.

EDIT (comment response):

I also tried $cachefile = $_SERVER["DOCUMENT_ROOT"] . "/users/{$user}.php" to no avail.

Matt
  • 1,500
  • 8
  • 23
  • 38
  • 2
    You can't open a remote URL for writing over HTTP. If it's your own server, use a relative path. – Ry- May 14 '12 at 02:44
  • 1
    You can't chmod a remote file. If that file is on your local server, then use a NON-url path to point to it. – Marc B May 14 '12 at 02:46

3 Answers3

2

You cannot write to a remote file using fwrite(). If you're writing to a file that is on your own server, use a relative path.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1

You need to use the full path to the file on the server, not the web address:

$cachefile = "/var/www/path/to/users/{$user}.php"; 
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

You cannot open remote files, you can only edit files on the local filesystem, if the file is on the file system, then you have to use the relative path,

Also instead of using fopen, you could just use file_get_contents() and file_put_contents().

Ry-
  • 218,210
  • 55
  • 464
  • 476
Mnkras
  • 300
  • 1
  • 7