У меня есть файл 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-код:
1-й код:
$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
2-й код:
$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);
3-й код:
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
4-й код:
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
5-й код:
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);
Других решений пока нет …