简体   繁体   English

使用PHP以编程方式查找wget位置?

[英]Find wget Location Programmatically using PHP?

Is it possible to determine where the wget binary is located using PHP? 是否可以使用PHP确定wget二进制文件的位置? I'm trying to create a softcoded wget cron command for my users. 我正在尝试为我的用户创建一个软编码的wget cron命令。 I know that in most environments, the wget location will already be included in the PATH but cron doesn't always share the same environment. 我知道在大多数环境中, wget位置已经包含在PATH但是cron并不总是共享同一环境。 To play it safe, I want to use an absolute path like /usr/bin/wget . 为了安全起见,我想使用/usr/bin/wget类的绝对路径。

Is there a constant similar to PHP_BINDIR ? 是否有类似于PHP_BINDIR的常量? I cannot assume wget will be in the same location as PHP, right? 我不能假设wget将与PHP位于同一位置,对吗? Any other ideas? 还有其他想法吗?

Thanks for any help! 谢谢你的帮助!

That's why we have env , which is guaranteed to be in /usr/bin , so all you have to do is: 这就是为什么我们有env ,它保证在/usr/bin ,因此您要做的就是:

/usr/bin/env wget [options] [url]

And you're good to go, provided the user running the PHP script has the correct permissions to execute wget , write the downloaded files to the specified path etc... but that's a different matter 只要您运行PHP脚本的用户具有执行wget的正确权限,将下载的文件写入指定的路径等,就可以了。但是那是另一回事

On the other hand, because you've tagged this question with the crontab tag, make sure the hashbang at the top of your script uses env , too: 另一方面,由于您已使用crontab标记标记了该问题,因此请确保脚本顶部的hashbang也使用env

#!/usr/bin/env php
<?php
//your script here

Then, wget will be callable (again: if the environment variables, and permissions check out) plain and simple: 然后, wget可以被简单地调用(同样:如果环境变量和权限被检出):

#!/usr/bin/env php
<?php
exec('wget --help', $output);
var_dump($output);

Should yield zomething like: 应该产生类似的东西:

array(172) {
  [0]=>
  string(51) "GNU Wget 1.15, a non-interactive network retriever."
  [1]=>
  string(32) "Usage: wget [OPTION]... [URL]..."
  [2]=>
  string(0) ""
  [3]=>
  string(72) "Mandatory arguments to long options are mandatory for short options too."
  ...

Tried it on my machine, no problems whatsoever 在我的机器上试用过,没有任何问题

Is not a php command, but a unix command : which . 不是php命令,而是unix命令: which

Example : 范例:

~ $ which wget
/usr/bin/wget

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

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