1

i have a php cli command problem with Mamp pro. My problem is that the exec inside a php script and in the terminal are different. I explain:

  1. when i execute this command => which php

    In the mac terminal:

    I get this path: /Applications/MAMP/bin/php/php5.6.2/bin/php

    That is normal because i change the path in my .bash_profile file like this:

    export MAMP_PHP=/Applications/MAMP/bin/php/php5.5.18/bin
    export PATH="$MAMP_PHP:$PATH"
    

    In a navigator:

    with this line inside a php script: 
    <?php echo exec("which php"); ?>
    

    I get this path: /usr/bin/php

  2. Same result but more detailed with the command => php --ini

    In the mac terminal:

    Configuration File (php.ini) Path: /Applications/MAMP/bin/php/php5.5.18/conf
    Loaded Configuration File:         /Applications/MAMP/bin/php/php5.5.18/conf/php.ini
    Scan for additional .ini files in: (none)
    Additional .ini files parsed:      (none)
    

    In a navigator:

    Configuration File (php.ini) Path: /etc
    Loaded Configuration File:         /etc/php.ini
    Scan for additional .ini files in: /Library/Server/Web/Config/php
    Additional .ini files parsed:      /Library/Server/Web/Config/php/php.ini
    
  3. And the more strange is when in write a php file with this code inside: <?php var_dump(shell_exec("which php -v")); phpinfo(); ?> and i launch this script in the terminal and in the navigator too. I got this:

    In the mac terminal:

    Launch with : php -f /Users/tm/Sites/test.com/test.php

    Result: /Applications/MAMP/bin/php/php5.5.18/bin/php PHP Version => 5.5.18

    In a navigator:

    Result: /usr/bin/php PHP Version => 5.5.18

So it seems that a php script launched from the navigator use the right version of php because the php info return the right version (5.5.18) but the cli of this php script use another php and this is my problem. But when this script is launched from the terminal all is as expected.

Thomas Melcer
  • 163
  • 2
  • 13

1 Answers1

1

The problem you're running into is that Apache does not use the same BASH-based PATH configurations that your Mac OS X Terminal, under your user account, uses. To say it another way, the "PATH" value that you setup by configuring your specific login user .bash_profile entry, is specific to the BASH shell (Terminal) processes for your user, while your web-run page is being run under Apache (i.e. it doesn't load your user account's BASH settings).

The simple way to end up with the same binary (on a per-PHP script basis), if you're going to be using exec() would be to do it something like this:

<?php 
    echo "Default PHP path: " . exec('which php') . PHP_EOL;
    $current_path = exec('echo $PATH');
    $your_custom_php_path = '/Applications/MAMP/bin/php/php5.5.18/bin';
    putenv('PATH=' . $your_custom_php_path . ':' . $current_path);
    echo "New PHP path: " . exec('which php') . PHP_EOL;
?>

(Note: above script is formatted for shell output, not for web output, in terms of line breaks).

This basically configures your PHP script, on the fly, to prepend (and use) the same PATH setting value that you're using in BASH, but within the context of your Apache-run PHP script.

There are other ways of accomplishing the same thing (Apache-level configs, etc) - but hopefully this explains why you're seeing the different values, and gives you one way to get around the issue.

twykr
  • 31
  • 5