2014-12-27 68 views
0

只是为了好玩,我试图让从PHP CLI(在/ usr/bin中/ PHP的-a)wget的下载和它的作品:为什么wget backticks shell命令在PHP CLI中工作,而不是在PHP脚本中工作?

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:请不要告诉我,我可以用卷曲对于这样的任务,我知道我可以,我只是好奇,想知道我可以做同样的事情,而不使用PHP工具,这是它

+0

PHP日志中是否有错误? – Barmar 2014-12-27 16:04:11

+0

也许'wget'不在apache用户的'$ PATH'中? – Barmar 2014-12-27 16:05:04

+0

@Barmar不,在php错误日志中没有任何错误,我已经检查过'wget'应该在路径中,因为apache用户和我用来运行'php' CLI命令的用户是一样的 – tonix 2014-12-27 16:07:13

回答

0

反引号操作符运行一个shell命令并从shell命令返回输出。

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

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

trigger_error($result, E_USER_NOTICE); 
+0

它没有帮助,$ result是一个空字符串。我发现了错误,请检查我的评论:) – tonix 2014-12-27 16:27:57

+0

如果它的路径问题$ result应该是包含“sh:wget:command not found”的字符串 – Stephen 2014-12-27 19:31:35

+0

是的,但我不知道为什么我没看到但是,任何错误。也许是因为'sh:wget:command not found'被写入stderr? – tonix 2014-12-27 19:38:26

1

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

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

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

+0

谢谢你的建议! – tonix 2014-12-27 17:08:45

相关问题