Jump to content


Photo

putenv() doesn't work


  • Please log in to reply
4 replies to this topic

#1 webhead

webhead
  • Members
  • PipPip
  • Member
  • 23 posts
  • LocationOH

Posted 16 October 2006 - 05:49 PM

Hi, I'm trying to do a mysqldump from php. I can do so from Terminal but not php.

Phpinfo shows the PATH to be /bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec:/System/Library/CoreServices

but I need to add /usr/local/mysql/bin/ so it can find mysqldump.

I made sure safe mode is off and tried various ways to set the mysqldump command, but it just can't find it. Here is what I tried with putenv:
$myPath = "PATH=/bin /sbin /usr/bin /usr/sbin /usr/libexec /System/Library/CoreServices /usr/local/mysql/bin/";
$chgPath = putenv($myPath); 
echo "<br>return value of putenv = ",$chgPath; echo "<br>my path = ",$myPath;
echo "<br>returned path = "; echo getenv('PATH');
Help!

#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 16 October 2006 - 07:06 PM

The paths are separated by colons (":").
putenv('PATH=/path/to/mysql/bin:'.getenv('PATH'));
echo getenv('PATH');

You should consider setting a variable to hold the path to the binary instead of using the PATH environ var.

eg
$mysqldump = '/path/to/mysql/bin/mysqldump';
$command = "$mysqldump --version";
passthru($command);


#3 webhead

webhead
  • Members
  • PipPip
  • Member
  • 23 posts
  • LocationOH

Posted 16 October 2006 - 08:16 PM

Thanks! Progress made but still no output file from mysqldump.

What it's doing now is showing that it found mysqldump after the putenv statement, but the getenv is unchanged. Here's my code:
putenv('PATH=/usr/local/mysql/bin:' . getenv('PATH'));
echo "getenv is --> ", getenv('PATH');
echo "<br /><pre>" . shell_exec('which mysqldump') . "</pre>";
$mysqldump =  "/usr/local/mysql/bin/mysqldump";
$command = "$mysqldump --version";
passthru($command);
$dumpit = "$mysqldump -h $h -u $u -p$p $d > $filedest";  
passthru($dumpit);

And the displayed messages:

getenv is --> /bin:/sbin:/usr/bin:/usr/sbin:/usr/libexec:/System/Library/CoreServices

/usr/local/mysql/bin/mysqldump     (result of $command)

mysqldump Ver 10.10 Distrib 5.0.24a, for apple-darwin8.5.1 (i686)


#4 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 16 October 2006 - 10:03 PM


"/usr/local/mysql/bin/mysqldump" should be the result of "which mysqldump"

I've also noticed that when run through apache "getenv()" does not show the new PATH. The results show that the directory is a part of the new PATH however. The output of phpinfo after the putenv() call also shows the dir in the PATH under "Environment".

When run on the command line the output of getenv reflects the change.

Progress made but still no output file from mysqldump.


Does apache have write access to the directory you're trying to dump to?

If you're sure that apache has write access to the directory you can use the following to put the errors in a file called dump_err.txt
$errdest = '/path/to/dir/dump_err.txt';
exec("$mysqldump ... > $filedest 2> $errdest", $output, $return);
if ($return)
{
    echo file_get_contents($errdest);

}

I recommend you run this script from the command line instead of through apache.

#5 webhead

webhead
  • Members
  • PipPip
  • Member
  • 23 posts
  • LocationOH

Posted 16 October 2006 - 11:00 PM

Does apache have write access to the directory you're trying to dump to?

DOH!!

I could have sworn I set the chmod for that directory. The weird thing is that it wrote to that directory from Terminal. But since all is well now, no more swearing for me. ;-)

Thanks loads!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users