2012-05-14 123 views
-1

我有以下代码:写入文件PHP

$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); 

文件路径是无效的,因为它产生这个错误:Warning: chmod() [function.chmod]: No such file or directory in ...

然而,这段代码:

$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); 

文件 - 使用第二个代码块 - 与创建它的脚本创建在相同的文件夹中。不过,我想将它保存到第一个区块的位置。我在php.net上寻找fopen,我没有看到我做错了什么,但显然我是。

EDIT(注释响应):

我也试过$cachefile = $_SERVER["DOCUMENT_ROOT"] . "https://stackoverflow.com/users/{$user}.php"无济于事。

+2

您无法打开通过HTTP写入的远程URL。如果它是您自己的服务器,请使用相对路径。 – Ryan

+1

你不能chmod一个远程文件。如果该文件位于本地服务器上,则使用非URL路径指向它。 –

回答

1

你需要使用的完整路径的文件服务器上,而不是网址:

$cachefile = "/var/www/path/to/users/{$user}.php"; 
2

你不能写使用fwrite()远程文件。如果您正在写入位于您自己的服务器上的文件,请使用相对路径。

0

无法打开远程文件,你只能在本地文件系统中编辑文件,如果文件是在文件系统上,那么你必须使用相对路径,

而且不是使用的fopen,你可以只需使用file_get_contents()file_put_contents()即可。