参考
项目 | 描述 |
---|---|
搜索引擎 | Bing、Google |
AI 大模型 | 文心一言、通义千问、讯飞星火认知大模型、ChatGPT |
PHP 官方 | filesystem.configuration.php |
PHP 官方 | PHP Manual |
环境
项目 | 描述 |
---|---|
PHP | 5.5.0 、5.6.8 、7.0.0 、7.2.5 、7.4.9 、8.0.0 、8.2.9 |
PHP 编辑器 | PhpStorm 2023.1.1(专业版) |
allow_url_fopen
allow_url_fopen 配置项
allow_url_fopen
是 PHP 中的一个配置选项,它决定了 PHP 是否能够通过 URL (而非本地文件路径)
来打开文件。这个配置选项的值会影响到一些 PHP 中与文件操作相关的函数的行为,例如 fopen()
和 file_get_contents()
。具体来说,当 allow_url_fopen
被设置为 On
(开启)时,这些函数可以用来 读取
或 写入
远程文件
。而当该配置项被设置为 Off
(关闭)时,这些函数 只能用于操作本地文件
。
操作远程文件
当 allow_url_fopen
选项被设置为 On
时(目前,allow_url_fopen
选项在每一个 PHP 版本中都是 默认开启
的),PHP 能够访问和处理远程文件。例如,你可以使用 file_get_contents()
函数来读取一个远程网站的 HTML 内容。对此,请参考如下示例:
<?php
# 通过 file\_get\_contents() 函数获取
# 百度页面的 HTML 内容。
$content = file\_get\_contents('http://www.baidu.com');
var\_dump($content);
执行效果
上述示例的部分输出内容如下:
string(9508) "<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta content="always" name="referrer"><meta name="description" content="全球领先的中文搜索引擎、致力于让网民更便捷地获取信息,找到所求。百度超过千亿的中文网页数据库,可以瞬间找到相关的搜索结果。"><link rel="shortcut icon" href="//www.baidu.com/favicon.ico" type="image/x-icon"><link rel="search" type="application/opensearchdescription+xml" href="//www.baidu.com/content-search.xml" title="百度搜索">
<title>百度一下,你就知道</title><style ...
file 协议
在 PHP 中,file
协议的使用不受 allow_url_fopen
配置项的控制。对此,请参考如下示例:
<?php
# 通过 allow\_url\_fopen 函数获取
# allow\_url\_fopen 配置项的值。
var\_dump(ini\_get('allow\_url\_fopen'));
# 尝试使用 file\_get\_contents 获取当前主机路径
# C:\Users\Public\Documents\index.php 中的内容
var\_dump(file\_get\_contents('file:///C:\Users\Public\Documents\index.php'));
执行效果
由于 allow_url_fopen
配置项在 PHP 中默认是开启的,故在执行上述示例前请将 allow_url_fopen
配置关闭(可通过修改 PHP 配置文件 php.ini
实现)。
由于 allow_url_fopen
配置已被关闭,故首个 var_dump
语句的输出为 string(0) ""
。在 allow_url_fopen
配置被关闭的状况下,file_get_contents
等函数仍能通过 file
协议对本机文件进行操作。
string(0) ""
string(35) "<?php
var\_dump('Hello World');"
allow_url_include
allow_url_include 配置项
allow_url_include
是 PHP 的一个配置指令,与 allow_url_fopen
类似,但 allow_url_include
配置专门针对 PHP 的 include
、include_once
、 require
及 require_once
语句。当 allow_url_include
被设置为 On
时,PHP 允许通过 URL 的形式,从远程服务器 包含和执行
PHP 文件。对此,请参考如下示例:
http://192.168.1.8/target
首先,我在 IP
地址为 192.168.1.8
的服务器中准备了文件 target
,在当前主机中通过浏览器访问该页面的效果如下:
开启 allow_url_include 选项
由于在 PHP8.0.0
版本中,allow_url_include
选项默认是关闭的,故需要通过修改配置文件来开启该选项。将 php.ini
配置文件中的:
allow_url_include = Off
修改为如下内容并对其进行保存。
allow_url_include = On
执行如下示例代码
<?php
include('http://192.168.1.8/target');
执行效果
由于 allow_url_include
配置项在 PHP7.4.0
版本被废弃,故在开启并使用到 allow_url_include
时,PHP 将输出提示信息 PHP Deprecated: Directive 'allow_url_include' is deprecated in Unknown on line 0
。
在执行上述示例代码后,target
文件中的内容被 包含至当前文件且被作为 PHP 代码进行执行
。
PHP Deprecated: Directive 'allow\_url\_include' is deprecated in Unknown on line 0
string(11) "Hello World"
若在执行上述示例代码前,未将 allow_url_include
配置项开启,则执行结果将为如下内容:
PHP Warning: include(): http:// wrapper is disabled in the server configuration by allow\_url\_include=0 in C:\index.php on line 4
PHP Warning: include(http://192.168.1.8/target): Failed to open stream: no suitable wrapper could be found in C:\index.php on line 4
PHP Warning: include(): Failed opening 'http://192.168.1.8/target' for inclusion (include_path='.;C:\php\pear') in C:\index.php on line 4
allow_url_include 与 allow_url_fopen
区别
在开启 allow_url_fopen
配置项后,PHP 仅能够对远程文件进行读写等文件操作
。
在开启 allow_url_include
配置项后,PHP 将能够通过 include
等函数 将远程文件包含至当前文件并将其作为 PHP 代码进行执行
。
举个栗子
<?php
$target\_url = 'http://192.168.1.8/target';
var\_dump(file\_get\_contents($target\_url));
include($target\_url);
执行效果
在执行上述示例代码前,请将 allow_url_fopen
与 allow_url_include
配置项开启。
由于被 allow_url_fopen
配置项影响的 file_get_contents()
函数仅能够对远程文件执行读写等文件操作,故 http://192.168.1.8/target
中的代码仅被执行了一次。
PHP Deprecated: Directive 'allow\_url\_include' is deprecated in Unknown on line 0
string(33) "<?php
var\_dump('Hello World');
"
string(11) "Hello World"
联系
allow_url_include
的生效依赖于 allow_url_fopen
配置项的开启。具体而言,当 allow_url_include
与 allow_url_fopen
两个配置项均被开启时,allow_url_include
才能够发挥作用。若仅有 allow_url_include
配置项被开启,则无法发挥 allow_url_include
配置项所起到的功能。