Published by admin on 08 Jul 2008
How to call linux shell commands from php script
The other day I wanted to convert MS Word document to text on my VPS. I knew that someone somewhere has already written a program to do just that, and sure enough, some kind folks on the forum mentioned Antiword. Now all we need is to use that prog in our php script using one of many system program execution functions.
And this is where I hit a roadblock: no matter how I tried, not only I could not get the program to work, I could not even get an error message that would tell me what’s wrong. To cut the long story short, you need to add “2>&1″ to your shell command, like this:
$output=shell_exec('command 2>&1');
The “2>&1″ bit tells to pipe the standard error to the standard output. Otherwise the standard error may not be outputted by the system and you won’t have a way to troubleshoot your script. Read more about what does “> /dev/null 2>&1? means.
Voila. Now my $output variable contains the program output, be it an error or successful execution.