I want to run a executable file from my php script and the command used in the command line is
where, profit is the executable file and profit.in is the input file. I wrote the following php script for running The same program when ran in the command prompt
Asked
Active
Viewed 121 times
0

Pari.
- 27
- 1
- 7
-
try it with `system` function – Miqdad Ali Jul 04 '12 at 10:43
-
Where is your profit.in file? when your PHP script is running are you sure the profit.in file is in the same directory your php script thinks its running from? – BugFinder Jul 04 '12 at 10:46
-
@BugFinder yes i looked at that long before... both profit.in and the php script is in same folder – Pari. Jul 04 '12 at 10:56
-
and you're running php from the command line? – BugFinder Jul 04 '12 at 11:00
-
@BugFinder no i m running the program using browser – Pari. Jul 04 '12 at 11:21
1 Answers
0
exec doesn't return the result to $profit
, try it this way:
exec('C:/xampp/htdocs/example/profit.exe < profit.in',$results,$status);
if ($status === 0)
{
var_dump($results);//will be an array
}
Also make sure that the path to your parameter file is correct, best use an absolute path if it's not being generated by your script.
Notes
Since your exec
calls leads me to believe you're working on a windows box, the path separator is a backslash, which you should escape, too:
exec('C:\\xampp\\htdocs\\example\\profit.exe < profit.in',$results,$status);
is what it should look like, imo.
A second possible issue just came to mind: you're using xampp, on a windows machine. If you're running this script from the command line, make sure you're using the right exe: php-cli.exe
, instead of the php.exe
:

Elias Van Ootegem
- 74,482
- 9
- 111
- 149
-
Thanls @Elias :) but the output prints as a array... how can i remove the array and print the normal output as a string? – Pari. Jul 04 '12 at 11:28
-
Any way you like, it's just an array like any other. If you need it printed out in your console/ command prompt: `echo implode('"\n",$results);` Here, double quotes are important, when using single quotes `\n` will be treated as a string, not a line feed. You could also loop through the array (`while($line = array_shift($results))` or `for ($i=0;$i
– Elias Van Ootegem Jul 04 '12 at 13:22 -
Glad to help, just noticed your comment, if you're running this script on a browser, try this with the results array: `echo implode('
',$results);` – Elias Van Ootegem Jul 06 '12 at 06:55