Page 1 of 1

stdout redirect in exec function

Posted: Fri Mar 20, 2009 7:34 am
by tomolas
Hi.
I have a little problem redirecting the output of program called from exec function in php.
Simple example:

Code: Select all

 
$output = shell_exec('ls -al &>my_file.txt');
 
ls -al will of course be replaced with something else later.
I know that the output this produces will be in $output BUT, i don't know why it's not in the my_file.txt file.
I need the output to be writen to a file while it's produced. I don't need it after to process has finished.
I will have some other program reading my_file.txt while it's being writen to by some other process.

If you try ls -al &> my_file.txt in your console, it will work. But not if you do it this way in php.
Do you have any idea why this doesn't work in php ?

Thank you!

Re: stdout redirect in exec function

Posted: Fri Mar 20, 2009 7:47 am
by VladSun
Try:

Code: Select all

$output = shell_exec('ls -al  2>&1 > my_file.txt');
and see what errors you get, if any.

Also, in general you don't need the & :

Code: Select all

$output = shell_exec('ls -al  > my_file.txt');
And last - use absolute paths.