无法通过httpd找到PHP扩展名,但是可以从CLI中找到,并且具有相同的php.ini [英] PHP extensions not found via httpd but are found from CLI, with the same php.ini

查看:118
本文介绍了无法通过httpd找到PHP扩展名,但是可以从CLI中找到,并且具有相同的php.ini的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在安装PHP 7.1和Apache 2.4之后以及在Windows 7上,我想使用PHP 7.1的某些扩展.我编写了一个小的测试脚本index.php来调用给定扩展的某些功能.

<?php
    var_dump(mb_strlen('p'));
    var_dump(mysqli_connect_error());

,并取消注释php.ini中的相应行,例如

...
;extension=php_ldap.dll
extension=php_mbstring.dll
;extension=php_exif.dll      ; Must be after mbstring as it depends on it
extension=php_mysqli.dll
;extension=php_oci8_12c.dll  ; Use with Oracle Database 12c Instant Client
...

并根据php.ini

的Windows特定部分配置extension_dir

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
; extension_dir = "ext"

extension_dir = "ext" 

我重新启动了Apache Web服务器,并获取了 http://localhost/index.php .我收到错误消息:

致命错误:未捕获错误:调用C:\ Program Files \ Apache24 \ htdocs ...中未定义的函数mb_strlen()...

这意味着未加载该扩展名.

我仔细检查了进行修改的php.ini与浏览器中显示的phpinfo()相同:

Loaded Configuration File    C:\Program Files\php\php.ini 

相反,如果我不是通过Web服务器而是通过命令行启动相同的index.php,那么我会得到不同的输出:

C:\Program Files\Apache24\htdocs>"C:\Program Files\php\php.exe" index.php
int(1)
NULL

这表示扩展程序已正确加载.

命令行PHP使用相同的php.ini:

c:\Program Files\Apache24\htdocs>php -i|find "Loaded Configuration File"
Loaded Configuration File => C:\Program Files\php\php.ini

相同 php.ini文件怎么可能从命令行加载扩展名,但通过Web服务器使用时却不加载扩展名?

解决方案

与php.ini中的描述相反,extension_dir应该用完整路径指定,而不仅仅是相对路径.将php.ini中的行更改为

extension_dir = "ext"

extension_dir = "C:/Progra~1/php/ext"

并重新启动Web服务器解决了该问题.

注意:我使用DOS-8.3路径而不是"C:/Program Files/php/ext",因为根据php-7.1.11-Win32-VC14-x64.zip \ install.txt手册,它不喜欢包含空格的路径:

您可以选择其他位置,但路径中没有空格(例如C:\ Program Files \ PHP),否则某些Web服务器将崩溃.

我所指的路径不包含任何空格,因此我认为我没有做错任何事情.尽管如此,我不确定如果将php安装到默认目录c:\ php会如何工作,但是似乎定义完整路径是一种更安全的方法.

I wanted to use some extensions of PHP 7.1 after installing it and Apache 2.4 and on my Windows 7. I wrote a small test script index.php to call some functions of the given extensions.

<?php
    var_dump(mb_strlen('p'));
    var_dump(mysqli_connect_error());

and uncommented the appropriate lines from the php.ini, like

...
;extension=php_ldap.dll
extension=php_mbstring.dll
;extension=php_exif.dll      ; Must be after mbstring as it depends on it
extension=php_mysqli.dll
;extension=php_oci8_12c.dll  ; Use with Oracle Database 12c Instant Client
...

and configured the extension_dir according to the windows-specific part of the php.ini

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = "./"
; On windows:
; extension_dir = "ext"

extension_dir = "ext" 

I restarted the Apache web server and fetched the http://localhost/index.php. I got the error messages:

Fatal error: Uncaught Error: Call to undefined function mb_strlen() in C:\Program Files\Apache24\htdocs...

which means the extension was not loaded.

I doublechecked that the php.ini I made the modifications in is the same as the phpinfo() displayed in the browser:

Loaded Configuration File    C:\Program Files\php\php.ini 

On the contrary, if I started the same index.php not via web server but from the command line then I got different output:

C:\Program Files\Apache24\htdocs>"C:\Program Files\php\php.exe" index.php
int(1)
NULL

which means the extensions were loaded properly.

The command line PHP uses the same php.ini:

c:\Program Files\Apache24\htdocs>php -i|find "Loaded Configuration File"
Loaded Configuration File => C:\Program Files\php\php.ini

How can it be that the same php.ini file loads the extensions from command line but does not load them when used via the web server?

解决方案

Contrary to the description in the php.ini, the extension_dir should be specified with a full path, not just a relative one. Changing the line in php.ini from

extension_dir = "ext"

to

extension_dir = "C:/Progra~1/php/ext"

and restarting the web server solved the problem.

NB: I used the DOS-8.3 path instead of "C:/Program Files/php/ext" because according to the php-7.1.11-Win32-VC14-x64.zip\install.txt manual, it does not like paths containing spaces:

You may choose a different location but do not have spaces in the path (like C:\Program Files\PHP) as some web servers will crash if you do.

The path I referred to doesn't contain any spaces so I don't think I did anything wrong. Despite this, I am not sure how it would work if I had installed php to the default directory c:\php but it seems that defining the full path is a more secure way.

这篇关于无法通过httpd找到PHP扩展名,但是可以从CLI中找到,并且具有相同的php.ini的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆