sara sara - 1 year ago 53
PHP Question

how to execute win cmd from php code?

I have x.exe in this path:

and I want to execute this command:
blastdbcmd -db nr -entry all -outfmt "%g %T"

In Windows command line I do:

cd C:\inetpub\wwwroot\webclient\db\nucleotide
blastdbcmd -db nr -entry all -outfmt "%g %T"

How can I do this from php code. I know
will do it but, how to type the above two statement in

How to save the output of the command in a text file?
I try 3 statement:

$cmd = "blastdbcmd.exe -db nr -entry all -outfmt "%g %T" -out $text_files_path/NewSeq_$OldDatabaseFile$NewDatabaseFile.txt";


exec("C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe -db nr -entry all -outfmt "%g %T" -out $text_files_path/NewSeq_$OldDatabaseFile$NewDatabaseFile.txt");


exec("cd C:\inetpub\wwwroot\webclient\db\nucleotide && blastdbcmd.exe -db nr -entry all -outfmt \"%g %T\" -out $text_files_path/NewSeq_$OldDatabaseFile$NewDatabaseFile.txt");

but, NewSeq_$OldDatabaseFile$NewDatabaseFile.txt not generated. It seems that the command not executed!

What can I do?


Answer Source

Three options:

You can prepend the .exe with the path:

exec("C:\inetpub\wwwroot\webclient\db\nucleotide\blastdbcmd.exe -db nr -entry all -outfmt \"%g %T\"");
// don't forget to escape the quotes.

If your .exe for some reason really requires you to run it from that directory, change into it with chdir() before you execute it.

exec("blastdbcmd.exe -db nr -entry all -outfmt \"%g %T\"");

Alternatively you can change into the directory and run the command in a single line:

exec("cd C:\inetpub\wwwroot\webclient\db\nucleotide && blastdbcmd.exe -db nr -entry all -outfmt \"%g %T\"");