为什么我的 PHP 与 cmd 中指定的命令不同 运行?

Why my PHP doesn't run the same command as specified in cmd?

我正在使用 windows 机器。 当我 运行 在 cmd 中使用此命令时,它工作正常。

C:\wamp\www\upload\cprogram.exe > output.txt

但是当我在 php 中写入相同的命令时,它显示

"> not recognised as an internal or external command"

我的php代码:

$exepath="C:\wamp\www\upload\cprogram.exe";

$outputpath="C:\wamp\www\upload\output.txt";
exec("$exepath > $outputpath");

请告诉我如何发送我执行的 C 程序输出文件?

Please tell me how can i send my executed C program output a file?

使用 php.

可以通过多种方式将命令的输出写入文件

在您的示例中,由于使用了连接,它不起作用。这应该会更好:

exec($exepath.' > '.$outputpath);

另一种选择是改用 shell_exec 命令:

$exepath="C:\wamp\www\upload\cprogram.exe";
$outputpath="C:\wamp\www\upload\output.txt";
shell_exec ($exepath ." > ". $outputpath);

或者只使用 system 命令并自己将输出写入文件:

$exepath="C:\wamp\www\upload\cprogram.exe";
$outputpath="C:\wamp\www\upload\output.txt";
system($exepath, $return);
$fp = fopen($outputpath, 'w');
fwrite($fp, $return);
fclose($fp);