Search code examples
phpwindowsexecstdoutwampserver

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


I'm using a windows machine. When I run this command in cmd it works fine.

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

But when I write the same command in my php it shows

"> not recognised as an internal or external command"

My php code :

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

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

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


Solution

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

    There are many ways that you can write the output of your command to a file using php.

    In your example it doesnt work due to the use of concatenation. This should work better:

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

    Another option would be to use the shell_exec command instead:

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

    Or just use the system command and write the output to a file yourself:

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