Jump to content

putenv() doesn't work


webhead

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!
Link to comment
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]
Link to comment
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]
Link to comment
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.
Link to comment
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!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.