How to set a default php.ini path for PHP CLI on W

2019-02-27 10:53发布

问题:

Brief introduction to problem:

I need to load pdo_mysql to run command php app/console doctrine:database:create and other commands for Symfony2.

I found a way to do this by running php -c "path/to/my/php.ini" app/console doctrine:database:create

Problem:

Since I don't want to add the path to my php.ini everytime I run commands in PHP CLI, where/how can I setup Windows, so that everytime I type php somecommand in console it will load my desired php.ini file?

回答1:

What if you add path/to/your/php.ini to the path environment variable and then just run php -c "php.ini" app/console doctrine:database:create

Could you try that?



回答2:

Create a .CMD file which automatically runs PHP with the required options:

path/to/php.exe -c "path/to/php.ini" %1 %2 %3 % %5 %6 %7 %8 %9

and call it something like phpcli.cmd. Make sure it's on your search path and off you go. The only change you need to make is to run phpcli rather than php.



回答3:

There are several ways to do it, but if you don't want to mess with an alias or making multiple copies of php.ini, you can also set the PHPRC environment variable. I would think this is the recommended method to set it more "permanently".

More info in PHP documentation: http://php.net/manual/en/configuration.file.php

In Windows, an easy way to do this is to go to the "System Properties" dialog; either right-click on "My Computer" and click "Properties", or use the "System" item in Control Panel, then go to "Advanced" settings, click "Environment Variables", and click "Add" for either the system or your user, call it "PHPRC" and copy the path to your .ini file in there ... for example, mine was in C:\MAMP\conf\php5.6.28.

(this was on Win 7, they change some of the UI in different versions, but it's basically the same)

You can verify it's working by doing php --ini from the command line, output should be something like: Configuration File (php.ini) Path: C:\Windows Loaded Configuration File: C:\MAMP\conf\php5.6.28\php.ini Scan for additional .ini files in: (none) Additional .ini files parsed: (none) you can also do echo %PHPRC% from windows command prompt, or echo $PHPRC from Cygwin/bash/MinGW, etc. You will have to restart any existing terminal sessions for this to take effect, but in my experience it works for all three, since the bash environments also inherit the Windows environment variables.