I have a php file that works as expected when run from the CLI but when called by Chrome via a public ip address it echos but does not save the content or even create a file named cache.html.
<?php
$cacheFile = '/home/directories/php/cache.html';
ob_start();
echo '<h1>Hello world to cache</h1>';
$content = ob_get_contents();
ob_end_clean();
file_put_contents($cacheFile,$content);
echo $content;
?>
I have many php pages that work without issue but this is my first try at saving a webpage to file. I intend to mail the file to myself using PHPMailer. I was inspired by Chetan Sharma's answer at Save current page as HTML to server.
Both /etc/php7/cli/php.ini and /etc/php7/apache/php.ini are set on like this:
;;;;;;;;;;;;;;;;;;
; Fopen wrappers ;
;;;;;;;;;;;;;;;;;;
; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-fopen
allow_url_fopen = On
I am concerned about the warning given by the manual regarding ob_start as I serve by Apache; https://www.php.net/manual/en/function.ob-start.php
Warning Some web servers (e.g. Apache) change the working directory of a script when calling the callback function. You can change it back by e.g. chdir(dirname($_SERVER['SCRIPT_FILENAME'])) in the callback function.
I initially set the $cachefile = /cache.html
and set the output by
file_put_contents(chdir(dirname($_SERVER['SCRIPT_FILENAME']))$cachefile,$content);
That didn't work. However, $cacheFile = '/home/directories/php/cache.html';
works just fine in the CLI.
I have substituted ob_end_clean() for ob_end_flush() with no difference.
Why does it fail in the browser? I am curious but in the end I really want to save a lamp/php generated web page to a file in the local php-project directory with the formatting etc.. for use as an attachment by PHPMailer. This seemed my best way forward however the browser failure has me stumped.