我有一个cron脚本,需要将一些html写入文件。
需要写入 HTML 的文件位于public_html目录中,脚本位于该目录上方的不同位置。如果我手动CHMOD一切正常,但我不想将其保留为0777或继续手动执行,因此我想使我的脚本为我完成。
ob_start();
// build my HTML
$myHTML = ob_get_clean();
// CODE TO SAVE INTO INCLUDE <<<
$filename = 'home/public_html/file.php';
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w')) {
mail("support@domain.com", "Failed to create include", "Script failed to create include", "From: Support <support@domain.com>");
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, ($myHTML)) === FALSE) {
mail("support@domain.com", "Failed to create include", "Script failed to create include", "From: Support <support@domain.com>");
exit;
}
fclose($handle);
} else {
mail("support@domain.com", "Failed to create include", "Script failed to create include", "From: Support <support@domain.com>");
exit;
}
不幸的是,它似乎不起作用。为什么?
这
看起来像一个绝对的路径home/public_html/
你确定你没有拼错它吗?
您可以
var_dump(file_exists($filename))
如果返回true
,那么您可以尝试
跟
var_dump(file_put_contents($filename,$myHTML))
您可以检查在其中写入了多少字节。
如果返回false
则表示您遇到了权限问题
您必须先对文件具有写入权限,然后才能对权限进行更改。