Executing a command line program and getting output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nauman73
Forum Newbie
Posts: 1
Joined: Fri Sep 26, 2003 9:17 am
Location: Pakistan

Executing a command line program and getting output

Post by nauman73 »

Hi!

I am using exec() function to execute a command line porgram on my Windows machine. I want o get the output from execution and parse it. I pass an array to exec() function. An example call is:

Code: Select all

<?php
exec("dir", $output, $ret);
?>
Of course I am not using the "dir" command in my code.

My problem is that when the command executes properly and returns a code value of 0, the $output array is populated, just what I wanted. However, when the execution is unsuccessful and a code value of 1 is returned, $output array does not get populated. Unsuccessful execution of the same command from Command Prompt shows some kind of error message as output. I was expecting that exec() will copy that output in $output array, just like it does when execution is successful.

Looking for any help.

Thanks
Nauman
Post Reply