2011-12-02 13:03

如何在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


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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doupo5178 doupo5178 10年前

    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?

    点赞 评论 复制链接分享
  • dounaoji2054 dounaoji2054 10年前

    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.

    点赞 评论 复制链接分享
  • douyi4991 douyi4991 4年前

    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.

    点赞 评论 复制链接分享