2

我有一些将 html 文件写入文件夹的 php 脚本。这在 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

而且我不相信该链接会允许找到该文件,因为它不会显示在浏览器中。

4

1 回答 1

0

阅读服务器的 error_log 并使用错误消息来诊断问题。如果您在日志中发现错误,请在此处发布以获得更好的答案。

根据错误消息进行编辑:

您可以尝试将 $filename 硬编码到 fopen() 函数中,以测试 $filename 变量中文件的路径是否不正确。

您需要使用var_dump()$filename 是否正确之类的方法来计算 $fh 的值。一旦你通过 fopen 使用 wordpress 传递 $filename 的路径,很可能它的路径不正确。相反,您从 FALSE 的 fopen 传递了一条错误消息,指出它在尝试打开文件时没有获取任何数据。您可能只需要在 wordpress 试图打开它的地方创建一个具有正确名称的空白文件。

于 2013-05-03T16:02:09.637 回答