简体   繁体   English

为什么wget backticks shell命令可以在PHP CLI中工作,而不能在PHP脚本中工作?

[英]Why wget backticks shell command works in PHP CLI and not in a PHP script?

just for fun I am trying to make wget downloads from php cli (/usr/bin/php -a) and it works: 只是为了好玩,我正在尝试从php cli(/ usr / bin / php -a)下载wget,它的工作原理是:

php > `wget -q -nd -O /Users/user/path/to/file.html http:\/\/host.com/some_file.html`;
php > // a HTML file is downloaded and its content is placed into /Users/user/path/to/file.html

However, when I try to do the same thing from a PHP script, it does not work: 但是,当我尝试从PHP脚本执行相同的操作时,它不起作用:

<?php 
 `wget -q -nd -O /Users/user/path/to/file.html http:\/\/host.com/some_file.html`;
 // After requesting the script from the browser and after the script is executed, the file doesn't exist on the specified path

I would like to say that the user which executes apache and therefore PHP server side scripting is the same as the user which executes the php command from the command line (so I guess this should not be a problem of permissions). 我想说的是,执行apache并因此执行PHP服务器端脚本的用户与从命令行执行php命令的用户相同(因此我认为这不应该是权限问题)。

Why if I use a .php script and call wget inside the script the file is not saved? 为什么如果我使用.php脚本并在脚本内调用wget却未保存文件?

Thanks for the attention! 感谢您的关注!

PS: please, do not tell me that I can use curl for such a task, I know I can, I am just curious to know how can I do something similar without using PHP tools, that's it PS:拜托,不要告诉我我可以使用curl来完成这样的任务,我知道我可以,我只是想知道不使用PHP工具就可以做类似的事情,仅此而已

use the full path to wget , since the daemon doesn't run .bash_profile . 使用完整路径进行wget ,因为守护程序未运行.bash_profile

`/opt/local/bin/wget -q -nd -O /Users/user/path/to/file.html http://host.com/some_file.html`;

BTW, there's no need to escape / in shell commands. 顺便说一句,有没有必要逃跑/在shell命令。

The backtick operator runs a shell command and returns the output from the shell command. 反引号运算符运行shell命令并返回shell命令的输出。

In this situation, simply logging (or if you have to, echo-ing) the result will probably reveal the error. 在这种情况下,只需记录(或必须回显)结果,就可能发现错误。 Eg: 例如:

$result = `wget -q -nd -O /Users/user/path/to/file.html http:\/\/host.com/some_file.html`;

trigger_error($result, E_USER_NOTICE);

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

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