简体   繁体   English

为什么 php 文件在 CLI 中有效,而在浏览器中无效?

[英]Why does php file work in CLI but not in browser?

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 文件,它在从 CLI 运行时按预期工作,但是当 Chrome 通过公共 IP 地址调用它时,它会回显但不保存内容,甚至不创建名为 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.我有许多 php 页面可以正常工作,但这是我第一次尝试将网页保存到文件。 I intend to mail the file to myself using PHPMailer.我打算使用 PHPMailer 将文件邮寄给自己。 I was inspired by Chetan Sharma's answer at Save current page as HTML to server .我的灵感来自 Chetan Sharma 在Save current page as HTML to server的回答。

Both /etc/php7/cli/php.ini and /etc/php7/apache/php.ini are set on like this: /etc/php7/cli/php.ini 和 /etc/php7/apache/php.ini 都这样设置:

;;;;;;;;;;;;;;;;;;
; 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;我担心手册中关于 ob_start 的警告,因为我由 Apache 提供服务; https://www.php.net/manual/en/function.ob-start.php https://www.php.net/manual/en/function.ob-start.php

Warning Some web servers (eg Apache) change the working directory of a script when calling the callback function.警告 当调用回调函数时,某些 Web 服务器(例如 Apache)会更改脚本的工作目录。 You can change it back by eg chdir(dirname($_SERVER['SCRIPT_FILENAME'])) in the callback function.您可以通过回调函数中的例如 chdir(dirname($_SERVER['SCRIPT_FILENAME'])) 将其更改回来。

I initially set the $cachefile = /cache.html and set the output by我最初设置$cachefile = /cache.html并设置输出

file_put_contents(chdir(dirname($_SERVER['SCRIPT_FILENAME']))$cachefile,$content);

That didn't work.那没有用。 However, $cacheFile = '/home/directories/php/cache.html';但是, $cacheFile = '/home/directories/php/cache.html'; works just fine in the CLI.在 CLI 中工作得很好。

I have substituted ob_end_clean() for ob_end_flush() with no difference.我用 ob_end_clean() 代替 ob_end_flush() 没有区别。

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.我很好奇,但最后我真的想将一个灯/php 生成的网页保存到本地 php-project 目录中的文件中,并带有格式等。用作 PHPMailer 的附件。 This seemed my best way forward however the browser failure has me stumped.这似乎是我最好的前进方式,但是浏览器故障让我难住了。

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.因为 CLI 中的 php 在运行命令的用户(登录用户)的权限下运行,该用户肯定对该目录具有权限,因为您可能已经从 CLI 创建了该目录。

Change directory owner and permissions to the user running php in browser.将目录所有者和权限更改为在浏览器中运行 php 的用户。

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 https://serverfault.com/questions/125865/finding-out-what-user-apache-is-running-as

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'.不想将我的整个项目目录转到运行 apache 守护程序的“wwwrun”用户,我创建了一个名为“wwwrun”的新目录。 I then gave that user ownership, writable for all, "0777", and changed my script to point to the new directory.然后我赋予该用户所有权,所有人都可写,“0777”,并将我的脚本更改为指向新目录。 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.这允许我在我的用户下编辑项目,现在 wwwrun 有一个写入的地方,我的用户也可以访问它。 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';修改脚本: $cacheFile = '/home/directories/php/wwwrun/cache.html';

side question: Is this considered an unsafe practice?附带问题:这是否被认为是不安全的做法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM