Pregunta

I have some php script that writes an html file to a folder. This works perfectly outside of WordPress. But when I try to incorporate my code into a plugin, it won't write to a folder anymore. I've even tried writing it to the original folder I tested it in at the root directory which is outside of WordPress and it won't write to that either.

Here is the function that writes to an html file.

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;
 }

I've even tried

$fh = fopen($filename, 'a');
$fh = fopen($filename, 'a+');
$fh = fopen($filename, 'w');
$fh = fopen($filename, 'a+');

None of these work either.

I'm thinking that within WordPress it may be something in a php.ini file and or .htaccess files that needs to go in the folder I want to write to, but I'm stuck.

* UPDATE **

I found some errors in the error log and are as follows:

Error: fwrite() expects parameter 1 to be resource, boolean given in....

$fh = fopen($filename, 'a');//open file and create if does not exist
 fwrite($fh, $html);//write data
 fclose($fh);//close file

SECOND UPDATE

I added the following code and it now writes the file to the folder

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;
 }

But now when it calls to open the file it try's to open it from the following link

/var/chroot/home/content/##/########/html/wp-content/plugins/my-plugin/html/c413c4976260c1a786e7a48be03f3ad2.html

and I don't believe that link is going to allow the file to be found as it doesn't show up in the browser.

¿Fue útil?

Solución

Read your server's error_log and use the error message to diagnose the problem. If you find an error in the log, post it here for a better answer.

Edit based on error messages:

You can try hard-coding $filename into the fopen() function to test if the path to the file is incorrect in your $filename variable.

You'll need to figure out the value of $fh using something like var_dump() if $filename is correct. Most likely the path to $filename is not correct once you pass it in using wordpress through fopen. Instead you're passing in an error message from fopen of FALSE stating that it didn't get any data when it tried to open the file. It's possible that you just need to create a blank file with the proper name wherever wordpress is trying to open it.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top