如何通过PHP执行批处理文件?

时间:2013-09-30 07:11:13

标签: php batch-file command-line exec

我尝试在PHP中使用exec命令执行批处理文件。 我只是用它:

$filename = 'test.bat';
exec($filename);

但没有得到任何输出。我用另一个命令尝试了这个功能,它运行正常。 您的建议将受到高度赞赏。 感谢

6 个答案:

答案 0 :(得分:7)

主要问题是pathpermission。我已经让我的批处理文件执行了。

这是我的解决方案:

  1. 我从php文件所在的同一个文件夹中运行我的批处理文件。

    exec("mybatch.bat");

  2. 我确保Apache Service有足够的权限来运行批处理文件。只是为了测试我使用Apache的管理员帐户登录。

答案 1 :(得分:2)

Windows 服务器上注意引号。这对我有用:

system('cmd.exe /c C:\myfolder\_batches\run_this_batch.bat');

答案 2 :(得分:1)

system("cmd /c C:[path to file]");

As" RichieHindle"在一个类似的话题中说。

或尝试

exec("cmd.exe /c test.bat") ?

答案 3 :(得分:1)

如果您需要执行命令并将命令中的所有数据直接传回而不受任何干扰,请使用passthru()函数。

http://md1.php.net/manual/en/function.passthru.php

答案 4 :(得分:1)

我做的是以下内容:

  1. 创建了一个包含以下内容的PHP文件:

    $gotIt = array();
    $file = "getMyIP.bat";
    exec( $file, $gotIt );
    echo implode("<br>",$gotIt);
    
  2. 在同一文件夹中创建了一个批处理文件

    @ECHO off
    ipconfig
    
  3. 跑吧,等待防火墙跳过整个行动。

  4. 然后我得到了一个输出:

    Windows IP Configuration
    
    
    PPP adapter 3 USB Modem:
    
    Connection-specific DNS Suffix . :
    IP Address. . . . . . . . . . . . : ***.***.202.81
    Subnet Mask . . . . . . . . . . . : 255.255.255.255
    Default Gateway . . . . . . . . . : ***.***.202.81
    

    只有***是

    的数字

答案 5 :(得分:0)

正如exec doc

中所述
echo exec($filename);

exec($filename, $output);
echo $output;