我有以下代码:写入文件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"
无济于事。
您无法打开通过HTTP写入的远程URL。如果它是您自己的服务器,请使用相对路径。 – Ryan
你不能chmod一个远程文件。如果该文件位于本地服务器上,则使用非URL路径指向它。 –