Reputation: 80
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.
Upvotes: 1
Views: 1062
Reputation: 80
Yep, file permissions.
Not wanting to turn my whole project directory to the "wwwrun" user which runs the apache daemon, I created a new directory, named 'wwwrun'. I then gave that user ownership, writable for all, "0777", and changed my script to point to the new directory. Everything is resolved.
This allows me to edit the project under my user and now wwwrun has a place to write to which is accessible to my user as well. Thank-you for a second pair of eyes.
> md /home/directories/php/wwwrun
> sudo chown -R wwwrun wwwrun
> sudo chmod -R a+w wwwrun
Modify script: $cacheFile = '/home/directories/php/wwwrun/cache.html';
side question: Is this considered an unsafe practice?
Upvotes: 0
Reputation: 2620
Because php in CLI runs under the permissions of user running the command (logged in user), who surely has permissions to that directory, because you might have created this directory from CLI.
Change directory owner and permissions to the user running php in browser.
sudo chown -R your-apache-user /home/directories/php/
//apache-user you found as mentioned in question below
https://serverfault.com/questions/125865/finding-out-what-user-apache-is-running-as
Upvotes: 1