如何在Windows上为PHP CLI设置默认的php.ini路径?

如何在Windows上为PHP CLI设置默认的php.ini路径?

问题描述:

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?

问题简介: strong> p>

I 需要加载pdo_mysql来运行命令 php app / console doctrine:database:create code>以及Symfony2的其他命令。 p>

我找到了一种方法来运行 php -c“path / to / my / php.ini”app / console doctrine:database:create code> p>

问题: strong> p>

因为我不希望每次在PHP CLI中运行命令时都将路径添加到我的php.ini,我在哪里/如何设置Windows,以便我每次输入 在控制台中强> php somecommand code> 它会加载我想要的php.ini文件吗? strong> p> div>

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?

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.

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.