为什么 wget backticks shell 命令在 PHP CLI 中工作,而不是在 PHP 脚本中


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

只是为了好玩,我正在尝试从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

但是,当我尝试从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

我想说的是,执行apache并因此执行PHP服务器端脚本的用户与从命令行执行php命令的用户相同(所以我想这应该不是权限问题)。

为什么如果我使用.php脚本并在脚本中调用 wget 文件,则文件不会保存?

感谢您的关注!

PS:请不要告诉我我可以使用 curl 来完成这样的任务,我知道我可以,我只是好奇如何在不使用 PHP 工具的情况下做类似的事情,就是这样

使用完整路径来wget,因为守护程序不会.bash_profile运行。

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

顺便说一句,无需在 shell 命令中转义/

反引号运算符运行 shell 命令并返回 shell 命令的输出。

在这种情况下,只需记录(或者如果必须,回显)结果可能会显示错误。例如:

$result = `wget -q -nd -O /Users/user/path/to/file.html http:'/'/host.com/some_file.html`;
trigger_error($result, E_USER_NOTICE);