在运行 PHP 的 Apache 服务器上,我有一个始终返回 null 的命令。
exec("az storage container exists --account-name $accountName --account-key $key --name $containeurName", $output);
我修改了 sudoers 文件,但它没有改变任何内容:
www-data ALL=(ALL) NOPASSWD: /usr/bin/az
感谢您的帮助。
最佳答案
使用 exec($your_command, $output, $error_code)
并查看 $error_code
包含什么内容。这可能只是因为 az
不在 PHP 的 PATH 环境变量中。
尝试输入可执行文件的完整路径,通常如下所示:
<?php
// A default path in case "which az" doesn't work.
define('AZ_DEFAULT_PATH', '/usr/bin/az');
// Find the path to az with the "which" command.
$az_path = exec('which az');
if ($az_path === false) {
$az_path = AZ_DEFAULT_PATH;
}
// Sanitize your variables to avoid shell injection and build the command.
$cmd = $az_path .
"storage container exists --account-name $accountName " .
"--account-key $key --name $containeurName";
$last_line = exec($cmd, $full_output, $error_code);
// Then check if $last_line !== false and check $error_code to see
// what happened.
var_export([
'$cmd' => $cmd,
'$last_line' => $last_line,
'$full_output' => $full_output,
'$error_code' => $error_code,
]);
关于使用 azure cli 的 php exec 命令始终返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71529296/