NYCPHP Meetup

NYPHP.org

[nycphp-talk] exec() funciton help

Dan Cech dcech at phpwerx.net
Sat Aug 5 15:24:47 EDT 2006


Joseph Crawford wrote:
> I also tried setting the $result and the exec command returns 1

In command shell speak 1 means an error occurred.

The reason you're not getting any output is that you're redirecting it
to $file.

If your php process doesn't have permission to create the file, it will
just return 1.

You can get around this by doing 2 things.

firstly, touch() the file and chmod it appropriately from php, which
will allow you to react appropriately to any errors there

secondly, redirect stderr from the svn log command to stdout so that you
can see any errors it outputs and react to them:

1. use 1>&3 to 'save' stdout
2. redirect stdout to the output file with 1>filename
3. redirect stderr to the 'saved' stdout with 2>&3

Here is some example code:

$directory = '/home/httpd/vhosts/b2cdev.licketyship.com/httpdocs';
chdir($directory);

$file = 'svn.log';

if (!is_file($file) || !is_readable($file)) {
  if (!touch($file)) {
    echo 'error: couldn't touch '.$file;
    exit;
  }

  chmod($file,0644);

  exec('/usr/bin/svn log -v 1>&3 1>'.$file.' 2>&3',$output,$retval);
  if ($retval != 0) {
    echo 'error: '. implode("\n",$output);
    exit;
  }

  echo 'file created';
}

Dan



More information about the talk mailing list