【问题标题】:Executing bash script from php not working on Windows从 php 执行 bash 脚本无法在 Windows 上运行
【发布时间】:2012-08-10 15:31:13
【问题描述】:

我已经在 xampp ( ubuntu ) 下执行了这个脚本,它运行良好。但是当我尝试在 Windows 下执行它时,它没有给我任何错误,但也没有输出。

<? $command='ls -l'; $output = shell_exec($command); echo $output; ?>

PHP 不在保存模式下运行。问题是什么 ?它取决于操作系统吗?

【问题讨论】:

  • 简而言之,是的。有可用于 Windows 的 Bourne Shell 实现(以便您能够运行 bash 脚本)。 unxutils.sourceforge.net

标签: php windows bash xampp wampserver


【解决方案1】:

这是因为 ls 是基于 unix 的命令,在 Windows 上不可用

不显示错误是因为 shell_exec 只输出 STDOUT 而不是 STDERR,如果您希望能够查看错误,您可以像这样运行您的命令:

$output = shell_exec("{$command} 2>&1");

这将显示一个错误,指出找不到 ls 或类似的东西

在windows而不是ls中你可以运行dir

这可能会有所帮助:

<?
    $command=substr(PHP_OS, 0, 3)=='WIN'?'dir':'ls -l';
    $output = shell_exec("{$command} 2>&1");
    echo $output;
?>

【讨论】:

    猜你喜欢
    • 2016-02-18
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    相关资源
    最近更新 更多