Jump to content

Archived

This topic is now archived and is closed to further replies.

webhead

putenv() doesn't work

Recommended Posts

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:
[code]$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');[/code]
Help!

Share this post


Link to post
Share on other sites
The paths are separated by colons (":").
[code]
putenv('PATH=/path/to/mysql/bin:'.getenv('PATH'));
echo getenv('PATH');
[/code]

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

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

Share this post


Link to post
Share on other sites
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:
[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);[/code]

And the displayed messages:

[code]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) [/code]

Share this post


Link to post
Share on other sites

"/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.

[quote=webhead]
Progress made but still no output file from mysqldump.
[/quote]

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
[code]
$errdest = '/path/to/dir/dump_err.txt';
exec("$mysqldump ... > $filedest 2> $errdest", $output, $return);
if ($return)
{
    echo file_get_contents($errdest);

}
[/code]

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

Share this post


Link to post
Share on other sites
[quote author=shoz link=topic=111678.msg452842#msg452842 date=1161036201]Does apache have write access to the directory you're trying to dump to? [/quote]
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!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.