Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php exec:不返回输出_Php_Windows_Iis_Exec - Fatal编程技术网

php exec:不返回输出

php exec:不返回输出,php,windows,iis,exec,Php,Windows,Iis,Exec,我有一个问题: 在ISS web服务器上,安装了windows 7 x64 professional zend server。在php下运行此命令: exec('dir',$output, $err); $output为空,$err=1。因此exec没有重新执行输出,它似乎有一些错误。 Phpdisable_functions为空,Php未处于安全模式,为标准,我选中所有选项。 这似乎是一个普遍的错误,即使在谷歌上搜索也不会给出结果 请写下每个人的经验和最终的解决方案或解决方法。查看Apach

我有一个问题: 在ISS web服务器上,安装了windows 7 x64 professional zend server。在php下运行此命令:

exec('dir',$output, $err);
$output为空,$err=1。
因此exec没有重新执行输出,它似乎有一些错误。 Php
disable_functions
为空,Php未处于安全模式,为标准,我选中所有选项。 这似乎是一个普遍的错误,即使在谷歌上搜索也不会给出结果


请写下每个人的经验和最终的解决方案或解决方法。

查看Apache错误日志,也许你会发现错误消息

此外,您可以尝试使用popen而不是exec。它提供了更多的控制,因为您可以将进程启动和输出读取分开:

$p = popen("dir", "r");
if (!$p)
    exit("Cannot run command.\n");
while (!feof($p))
    echo fgets($p);
pclose($p);

PHP手册的相关章节中有一些帖子,例如:

我在使用PHP exec命令执行任何批处理时遇到问题 文件执行其他命令(即“dir”)效果良好。但是如果我 执行批处理文件时,我没有收到exec命令的输出

我的服务器设置由运行的Windows server 2003服务器组成 IIS6和PHP 5.2.3。在此服务器上,我有:

  • 已授予Internet用户在c:\windows\system32\cmd.exe上的执行权限
  • 授予Everyone->对写入批处理文件的目录的完全控制权
  • 授予Everyone->对整个c:\cygwin\bin目录及其内容的完全控制权
  • 已授予Internet用户“批量登录”权限
  • 指定要执行的每个文件的完整路径
  • 在服务器上测试了从命令行运行的这些脚本,它们工作正常
  • 确保%systemroot%\system32位于系统路径中
  • 事实证明,即使在服务器上安装了上述所有功能,我仍然可以 必须在exec调用中指定cmd.exe的完整路径

    当我使用调用时:
    $output=exec(“c:\\windows\\system32\\cmd.exe
    /c$batchFileToRun”)

    然后一切顺利。在我的情况下,
    $batchFileToRun
    是 批处理文件的实际系统路径(即调用 realpath())


    手册和手册页上还有一些。也许通过这些操作可以让它为您工作。

    出于安全目的,您的服务器可能限制了对“exec”命令的访问。在这种情况下,您可能应该联系托管公司以取消此限制(如果有)。

    您从命令(如
    dir
    )中获得零输出的主要原因是
    dir
    不存在。它是命令提示符的一部分,这个特殊问题的解决方案就在这个页面的某个地方

    您可以通过按
    WIN+R
    ,然后键入
    dir
    start
    来查找此问题-将显示一条错误消息

    无论这听起来多么反常,我发现在Windows中做任何与进程相关的事情最可靠的方法是使用组件对象模型。你说让我们分享我们的经验,对吗

    我听到人们在笑

    你恢复镇静了吗

    首先,我们将创建COM对象:

    $pCOM = new COM("WScript.Shell");
    
    然后,我们只运行需要运行的程序

    $pShell = $pCom->Exec("C:\Random Folder\Whatever.exe");
    
    酷!现在,它将一直挂起,直到二进制文件中的所有内容都完成。因此,我们现在需要做的是获取输出

    $sStdOut = $pShell->StdOut->ReadAll;    # Standard output
    $sStdErr = $pShell->StdErr->ReadAll;    # Error
    

    您还可以做一些其他事情-找出进程ID、错误代码等。虽然是Visual Basic,但它基于相同的方案。

    编辑:经过几次研究,我看到执行DIR命令的唯一方法如下:

    cmd.exe /c dir c:\
    
    因此,您可以使用以下方法尝试我的解决方案:

    $command = 'cmd.exe /c dir c:\\';
    
    您也可以使用proc_open函数,它允许您访问stderr、返回状态代码和stdout

        $stdout = $stderr = $status = null;
        $descriptorspec = array(
           1 => array('pipe', 'w'),  // stdout is a pipe that the child will write to
           2 => array('pipe', 'w') // stderr is a pipe that the child will write to
        );
    
        $process = proc_open($command, $descriptorspec, $pipes);
    
        if (is_resource($process)) {
            // $pipes now looks like this:
            // 0 => writeable handle connected to child stdin
            // 1 => readable handle connected to child stdout
            // 2 => readable handle connected to child stderr
    
            $stdout = stream_get_contents($pipes[1]);
            fclose($pipes[1]);
    
            $stderr = stream_get_contents($pipes[2]);
            fclose($pipes[2]);
    
            // It is important that you close any pipes before calling
            // proc_close in order to avoid a deadlock
            $status = proc_close($process);
        }
    
        return array($status, $stdout, $stderr);
    
    与popen或exec等其他功能相比,proc_open的有趣之处在于它提供了特定于windows平台的选项,如:

    suppress_errors (windows only): suppresses errors generated by this function when it's set to TRUE
    bypass_shell (windows only): bypass cmd.exe shell when set to TRUE
    

    您可以检查IIS用户对cmd.exe的权限

    cacls C:\WINDOWS\system32\cmd.exe
    
    如果输入输出类似于行(COMPUTERNAME=您的计算机名):

    这意味着用户没有执行cmd的权限

    您可以使用以下命令添加执行权限:

    cacls C:\WINDOWS\system32\cmd.exe /E /G COMPUTERNAME\IUSR_COMPUTERNAME:R
    

    我知道我已经选择了一个答案,因为我必须这样做,但我仍然不明白为什么它不能在我的macchine上工作,但我找到了一个回退(使用proc_open)解决方案,使用另一种方法:

    public static function exec_alt($cmd)
    {
        exec($cmd, $output);
        if (!$output) {
            /**
             * FIXME: for some reason exec() returns empty output array @mine,'s machine.
             *        Somehow proc_open() approach (below) works, but doesn't work at
             *        test machines - same empty output with both pipes and temporary
             *        files (not we bypass shell wrapper). So use it as a fallback.
             */
            $output = array();
            $handle = proc_open($cmd, array(1 => array('pipe', 'w')), $pipes, null, null, array('bypass_shell' => true));
            if (is_resource($handle)) {
                $output = explode("\n", stream_get_contents($pipes[1]));
                fclose($pipes[1]);
                proc_close($handle);
            }
        }
        return $output;
    }
    

    希望这对别人有帮助。

    我也有同样的问题,在花了很多时间和一杯咖啡之后

    仅在Windows7中禁用用户帐户控制(UAC) 短路径:P C:\Windows\System32\UserAccountControlSettings.exe 然后选择“从不通知”

    php exec()工作正常

    我使用: Apache版本:2.2.11
    PHP版本:5.2.8
    Windows 7 SP1 32位

    致以最良好的祝愿!:试试这个:

    shell_exec('dir 2>&1');
    

    它可以在启用UAC的Windows 8.1 64位上正常工作。

    如果您在Windows计算机上尝试运行如下可执行文件:

    shell_exec("C:\Programs\SomeDir\DirinDir\..\DirWithYourFile\YourFile.exe");
    
    如果没有输出或文件未启动,则应尝试以下操作:

    chdir("C:\Programs\SomeDir\DirinDir\..\DirWithYourFile");
    shell_exec("YourFile.exe");
    
    它应该会起作用

    如果使用脚本当前所在文件夹的相对路径,则此操作尤其方便,例如:

    $dir = dirname(__FILE__).'\\..\\..\\..\\web\\';
    

    如果您需要运行其他程序,请不要忘记将chdir()返回到原始文件夹。

    可能是因为出错了?:)检查错误1代码的含义…如果我在cmd shell上用php script.php启动相同的命令,它会工作,当我从浏览器(所以web服务器)启动它时,它不会工作,我真的不明白哪里出错了is@albanx:这会让我相信权限在您运行时有效,而在IIS运行时无效。@albanx请尝试将
    2>&1
    附加到您正在运行的命令中,看看是否可以看到任何错误消息。即使
    2>&1
    没有给出结果,此外,我认为这可能是权限问题,但我无法解决,我给iis用户所有可能的权限,但他在Windows上有iis服务器
    $dir = dirname(__FILE__).'\\..\\..\\..\\web\\';