我有一些PHP脚本将html文件写入文件夹。这完全适用于WordPress。但是当我尝试将我的代码合并到插件中时,它将不再写入文件夹。我甚至尝试将它写入我在WordPress之外的根目录中测试它的原始文件夹,它也不会写入。
这是写入html文件的函数。
function buildFrameFile($html, $filename){
$filename= '/wp-content/plugins/my-plugin/html/' . $filename . ".html";
$fh = fopen($filename, 'a');//open file and create if does not exist
fwrite($fh, $html);//write data
fclose($fh);//close file
return $filename;
}
我甚至尝试过
$fh = fopen($filename, 'a');
$fh = fopen($filename, 'a+');
$fh = fopen($filename, 'w');
$fh = fopen($filename, 'a+');
这些都不起作用。
我认为在WordPress中它可能是php.ini文件和/或.htaccess文件中需要进入我想要写入的文件夹中的东西,但是我被卡住了。
*更新**
我在错误日志中发现了一些错误,如下所示:
错误:fwrite()期望参数1是资源,在....中给出布尔值
$fh = fopen($filename, 'a');//open file and create if does not exist
fwrite($fh, $html);//write data
fclose($fh);//close file
第二次更新
我添加了以下代码,现在将文件写入文件夹
function buildFrameFile($html, $filename){
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
$filename= $DOCUMENT_ROOT. '/wp-content/plugins/my-plugin/html/' . $filename . ".html";
$fh = fopen($filename, 'a');//open file and create if does not exist
fwrite($fh, $html);//write data
fclose($fh);//close file
return $filename;
}
但现在当它调用打开文件时,尝试从以下链接打开它
/var/chroot/home/content/##/########/html/wp-content/plugins/my-plugin/html/c413c4976260c1a786e7a48be03f3ad2.html
我不相信链接会允许找到该文件,因为它没有显示在浏览器中。
答案 0 :(得分:0)
读取服务器的error_log并使用错误消息来诊断问题。如果您在日志中发现错误,请在此处发布以获得更好的答案。
根据错误消息进行修改:
您可以尝试将$ filename硬编码到fopen()函数中,以测试$ filename变量中文件的路径是否不正确。
如果$ filename正确,你需要使用像var_dump()
这样的东西来计算$ fh的值。一旦你通过fopen使用wordpress传递它,很可能是$ filename的路径不正确。相反,你传来一个来自FALSE的错误消息,说明它在尝试打开文件时没有获得任何数据。您可能需要在wordpress尝试打开它的地方创建一个具有正确名称的空白文件。