在 Windows 上安装 PHP 扩展

在 Windows 上有两种加载 PHP 扩展的方式:把扩展编译进 PHP,或者加载 DLL。加载预编译的扩展是更简单更被推荐的方式。

要加载某扩展,需要在系统中有其相对应的 ".dll" 文件。所有扩展都会由 PHP 小组定期自动编译(如何下载见下节)。

要将一扩展编译入 PHP,请参考 从源程序编译 一章。

要编译一个独立的扩展(即 DLL 文件),请参考 从源程序编译 一章。如果在 PHP 发行包和 PCEL 中都没有某 DLL 文件,那可能需要自己编译之后才能使用该扩展。

去哪里找扩展库?

PHP 扩展库通常称为"php_*.dll"(其中星号代表具体某扩展的名字),位于"PHP\ext"目录下。

PHP 发行包中包括了大多数开发者最常用到的扩展库。这些被称为"核心"扩展库。

不过呢,如果用户所需要的功能并没有被任何核心扩展提供,那还是有可能在 » PECL 中找到。PHP Extension Community Library(PECL,PHP 扩展社区库)是个 PHP 扩展的储存室,提供了对于所有已知扩展的下载及开发途径的指南。

如果用户开发了一个自己使用的扩展,可以考虑将其发布到 PECL 中以便于其他有相同需求的用户使用。一个很好的副作用是可以得到其他用户的反馈,感谢,错误报告甚至修正/更新。不过在向 PECL 发布扩展之前,请先阅读 » PECL 提交指南

下载哪个扩展?

用户常常会发现每个 DLL 都有好几个版本:

  • 不同的版本号(至少前两个数字要一致)
  • 不同的线程安全性设定
  • 不同的处理器体系(x86,x64,...)
  • 不同的排错设定
  • 其它

请记住用户的扩展设定应该与所使用的 PHP 可执行文件的设定都保持一致。以下脚本可以显示所有 PHP 设定:

Example #1 phpinfo() call

<?php
phpinfo
();
?>

或者在命令行运行:

drive:\\path\to\php\executable\php.exe -i

载入一个扩展

最常见的方式是在 php.ini 配置文件里包含一个 PHP 扩展。请注意很多扩展已经在 php.ini 里了,仅需要移除分号来激活它们。

需要注意,从 PHP 7.2.0 开始,可以直接用扩展的名称,来替代扩展的文件名。这样配置文件就跟操作系统解耦了,也更容易理解。我们推荐采用此方式加载扩展。为了与之前的版本保持兼容,直接写扩展名的方式会仍然支持。

;extension=php_extname.dll
extension=php_extname.dll
; PHP version 7.2 开始的推荐写法:
extension=extname
zend_extension=another_extension

不过呢,有些 web 服务器会搞混,因为其并不一定使用和 PHP 可执行文件处于同一目录下的 php.ini 文件。要搞清楚具体使用了哪一个 php.ini 文件,在 phpinfo() 的输出中查看:

Configuration File (php.ini) Path  C:\WINDOWS
Loaded Configuration File   C:\Program Files\PHP\5.2\php.ini

激活一个扩展后,保存 php.ini 文件并重启动 web 服务器,然后用 phpinfo() 再次查看确定。新的扩展应该有其自己的一节。

解决问题

如果某扩展并未在 phpinfo() 中显示,应该查看日志以确定问题出在哪里。

如果是在命令行使用 PHP(CLI),扩展加载出错信息会直接在屏幕显示。

如果在 web 服务器中使用 PHP,则日志文件的位置与格式各不相同。请阅读所使用的 web 服务器之文档以确定日志文件的位置,这与 PHP 本身并无关系。

最常见的问题是 DLL 文件的位置,php.ini 中"extension_dir"设定的值,以及编译时的设置不匹配。

如果问题出在编译时设置不匹配,那可能所下载的 DLL 文件不对。可以尝试重新下载一个设置匹配的扩展。此外,phpinfo() 可以起到很大帮助。

User Contributed Notes

peteb at airpost dot net 21-Jul-2020 03:23
This is not really true all the time:

"However, some web servers are confusing because they do not use the php.ini located alongside your PHP executable. To find out where your actual php.ini resides, look for its path in phpinfo():

Configuration File (php.ini) Path  C:\WINDOWS"

Ignore the path being C:\windows
This issue goes back over a decade. 
Php is hardwired to display "C:\windows" even though there is not any php.ini at that location. 
Moving php.ini to C:\windows will accomplish nothing. 
I have seen many posts about folks complaining that they can't get it to stop displaying "Path C:\windows".  I have not seen anything posted that fixes this and a few posts that claim it is hardwired to display this. 
I have both working and broken php setups on various windows machines and they all say this same "c:\windows" even though my php.ini file is in c:\php. 
I know apache is using the file c:\php\php.ini and not the windows directory because there is no php.ini in windows directory and changes to my php.ini file work fine even though the info from phpinfo is wrong on this line.
Chris 15-Jun-2018 09:09
In xampp. After removing the semicolon from the php.ini file, I had to stop the the apache server from the xampp GUI and restart it. Then I used the following code to view all the loaded extentions and my extension was shown there as well.

print_r(get_loaded_extensions());

Hope this helps!
Robert Dinion 15-May-2018 10:52
For Windows try setting the  extension_dir set to a complete path if you have an error.
 Only having
extension_dir ="ext"
did not work for me.
The Apache error log showed it looking for ext\\<extension.dll> and unable to find it.
; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
extension_dir = "C:\php\ext"
PHP8中文手册 站长在线 整理 版权归PHP文档组所有