繁体   English   中英

从 PHP 调用简单的 Golang 可执行文件失败,执行失败:“zip”:$PATH 中找不到可执行文件

[英]Calling a simple Golang executable from PHP failing with exec: “zip”: executable file not found in $PATH

尝试从 PHP 执行一个简单的 Golang 可执行文件


import (
    "fmt"
    "os/exec"
)

func main() {
    out, err := exec.Command("zip").Output()
    if err != nil {
        fmt.Println("Error Executing the command: \n")
        fmt.Printf("%s", err)
    } else {
        output := string(out[:])
        fmt.Println("Command Executed successfully with Output \n" + output)
    }

}

这是我的 php 调用脚本

<?php
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$output = shell_exec('zip 2>&1');
echo("Output of Calling zip command from PHP --> ".$output . "<br>");


$output = shell_exec('./genfilesdir/gotest  2>&1');
echo("Output of Calling zip command through golang --> ".$output);

?>

这里是PHP执行的output

Output of Calling zip command from PHP --> adding: -PK-�4PR��������- (deflated 0%) PKPK-�4PR�!-PK/M
Output of Calling zip command through golang --> Error Executing the command: exec: "zip": executable file not found in $PATH

当我尝试从命令行执行 golang 时,它可以工作

> sudo -u www-data ./gotest
Command Executed successfully with Output 
P�4PR��������-PP�4PR�!-PK/M
  • zip 在 /usr/bin 和 /usr/bin 中可用,在 $PATH 中用于 www-data
  • 当我直接从 PHP 调用“zip”时,它可以工作(参见上面的 php output)
  • 我不确定,为什么从 PHP 调用 golang 可执行文件时无法在路径中选择“zip”。

这是环境细节

Apache/2.4.41 (Ubuntu 20.04) PHP 8.0.2 (cli) (内置: Feb 14 2021 14:21:37) (NTS)

我错过了什么?

暂无
暂无

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

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