php - 如何使用 PHP 的参数执行 .exe 并将输出放入 txt 文件?

标签 php cmd exec exe

我有 PHP 文件,我想执行带有“参数”的 .exe 并将输出放在文本文件中。

当我从 Windows cmd 执行此工作时,它成功运行。

但是,当我在 PHP 中执行此任务时,文本文件是空的。我不知道执行是否有效,但文件是空的。

这是在 windows cmd 中运行的命令行:

cd C:\inetpub\wwwroot\webclient\db\nucleotide
blastdbcmd -entry $gi -db $DatabaseName -outfmt %f -out $text_files_path\result.txt

blastdbcmd是执行文件"blastdbcmd.exe"result.txt 使用命令的所有结果成功创建。

这是我的 PHP 代码: 第一个代码:

$gi = 1000232109;
$cmd = "-entry $gi -db $DatabaseName -outfmt %f";
exec("C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe $cmd" , &$output, &$return_var);
file_put_contents("$text_files_path/result.txt", $output);  //result.txt created
echo $return_var;    // 1 is printed

第二个代码:

$handle = popen('C:/inetpub/wwwroot/webclient/db/nucleotide/blastdbcmd.exe 2>&1', 'w');
$cmd = "-entry $gi -db $DatabaseName -outfmt %f";
exec("$cmd" , $output);
file_put_contents("$text_files_path/mm.txt", $output); //result.txt created
echo "hello";
pclose($handle);

第三个代码:

exec("C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe -db nr -entry $gi -outfmt %f -out $text_files_path/result.txt 2>&1"); //result.txt doesn't created

第四个代码:

exec("cd C:\inetpub\wwwroot\webclient\db\nucleotide && blastdbcmd.exe -db nr -entry $gi -outfmt %f -out $text_files_path/result.txt 2>&1"); //result.txt doesn't created

第五个代码:

exec("C:\\Windows\\System32\\cmd.exe /c \"C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe -entry $gi -db $DatabaseName -outfmt %f -out $text_files_path\result.txt\" 2>&1");  //result.txt doesn't created

编辑 1: 我测试这个:

$cmd = "-entry $gi -db $DatabaseName -outfmt %f";
exec("C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe $cmd" , &$out, &$return_var);
$output = "";
echo $out;      // "Array" is printed
foreach ($out as $line) {
echo $out;      // nothing printed
echo $line;      // nothing printed
$output .= $line;
}
file_put_contents("$text_files_path/result.txt", $output);
echo $return_var // 1 is printed

但是结果文件是空的。

我解决它:)

exec("cd C:\\inetpub\\wwwroot\\webclient\\db\\nucleotide && blastdbcmd.exe -entry $gi -db $DatabaseName -outfmt %f 2>&1",&$out, &$return_var);

只需将 \ 替换为 \\

感谢@budwiser 的“foreach 语句” 谢谢大家。

最佳答案

注意 $out 是一个数组:

exec("yourcommand.exe", $out);

$output = "";

foreach ($out as $line) {
    $output .= $line;
}

file_put_contents("C:\\Users\\MyUser\\Documents\\result.txt", $output);

编辑:

改变

exec("C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe $cmd", 
      &$out, &$return_var);

exec("C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe $cmd 2>&1",
      &$out, &$return_var);

关于php - 如何使用 PHP 的参数执行 .exe 并将输出放入 txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36736933/

相关文章:

php - 如何将一个小时添加到 0000-00-00 00 :00:00 format

php - 无法使用 laravel5 找到布局

php - MySQL 无法使用 FK 约束进行 INSERT

windows - 批量 IF 语句总是返回 false?

variables - 具有可变参数的 exec.Command

php - 如何比较 Auth::user()->id 和 userid 并在 Laravel 中运行 if else 命令

java - 从 Java 运行批处理,cmd 提示符消失

windows - 如何使用 subinacl 向所有用户授予权限?

C:传递数组代替变量参数列表

c - 从 exec 获取输出