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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8356708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:34:34  来源:igfitidea点击:

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

phpwindowsconfigurationcommand-line-interface

提问by ioleo

Brief introduction to problem:

问题简介:

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

我需要加载 pdo_mysql 来运行php app/console doctrine:database:createSymfony2 的命令和其他命令。

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

我找到了一种通过运行来做到这一点的方法 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 typephp somecommandin console it will load my desired php.ini file?

由于我不想每次在 PHP CLI 中运行命令时都将路径添加到我的 php.ini,我在哪里/如何设置 Windows,以便每次我php somecommand在控制台中输入它都会加载我想要的 php.ini 文件?

采纳答案by Iljaas

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

如果你添加path/to/your/php.ini到路径环境变量然后运行php -c "php.ini" app/console doctrine:database:create

Could you try that?

你可以试试吗?

回答by DaveyBoy

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

创建一个 .CMD 文件,它使用所需的选项自动运行 PHP:

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.

并将其称为phpcli.cmd. 确保它在您的搜索路径上,然后离开。您需要进行的唯一更改是运行 phpcli 而不是 php。

回答by Aaron Wallentine

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".

有几种方法可以做到,但是如果您不想弄乱别名或制作多个 php.ini 副本,您也可以设置 PHPRC 环境变量。我认为这是更“永久”设置它的推荐方法。

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

PHP 文档中的更多信息: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.

在 Windows 中,一个简单的方法是进入“系统属性”对话框;右键单击“我的电脑”并单击“属性”,或使用“控制面板”中的“系统”项,然后转到“高级”设置,单击“环境变量”,然后单击系统或您的用户,将其命名为“PHPRC”并将路径复制到您的 .ini 文件中……例如,我的在 .ini 文件中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)

(这是在Win 7上,他们在不同版本中更改了一些UI,但基本相同)

You can verify it's working by doing php --inifrom 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 $PHPRCfrom 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.

您可以通过php --ini从命令行执行来验证它是否正常工作,输出应该类似于: 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) 您也可以echo %PHPRC%从 Windows 命令提示符执行,或echo $PHPRC从 Cygwin/bash/MinGW 等执行。您必须重新启动任何现有的终端会话才能执行此操作效果,但根据我的经验,它适用于所有三个,因为 bash 环境也继承了 Windows 环境变量。