我有 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/