doujiong2533 2011-08-01 16:36
浏览 464
已采纳

在PHP脚本中添加$ PATH环境变量

I have a PHP script that is using wget to download some images. However, wget was installed using Homebrew so it's not available to the user running the PHP script. When I run exec('echo $PATH') I don't get the /usr/local/bin directory that contains wget. How do I add /usr/local/bin to the environment path so the PHP script can find wget?

Update: I forgot to mention the reason I can't specify the exact location is because the location may be different depending on which machine this script is being run on.

Solution:

This is what I ended up with:

//help PHP find wget since it may be in /usr/local/bin
putenv('PATH=' . getenv('PATH') . PATH_SEPARATOR . '/usr/local/bin');
if (exec('which wget') == null) {
    throw new Exception('Could not find wget, so image could not be downloaded.');
}

//now we know wget is available, so download the image
exec('wget ...');
  • 写回答

2条回答 默认 最新

  • doxrxwn2252 2011-08-01 16:41
    关注

    In order of preference:

    1. You can simply specify the full path /usr/local/bin/wget when you are calling the subprocess. This is probably the simplest and best approach.
    2. You can use proc_open instead of exec, which allows you to pass environment variables as an argument.
    3. You can use putenv to change the current environment (which will be inherited by subprocesses).
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料