How to execute .exe with arguments from PHP and put the output in a txt file?

I have a PHP file and I want to execute .exe with "arguments" and put the output in a text file.

When I do this job from cmd windows, it works successfully.

but when I completed this task in PHP, the text file is empty. I do not know if execution is working or not, but the file is empty.

Here is the command line that works in windows cmd:

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

blastdbcmdis a runtime file "blastdbcmd .exe". result.txtcreated successfully with all the result of the team.

Here is my PHP code: 1st code:

$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

Second code:

$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);

Third code:

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

4th code:

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

Fifth code:

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

Edit 1: I check this:

$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

but the result file is empty.

I allow it :)

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

just replacing \with\\

Thanks @budwiser for the "foreach instruction" Thanks to everyone.

+4
1

, $out - :

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

$output = "";

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

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

EDIT:

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);
0

All Articles