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.