警告:require_once():http://在服务器配置中禁用包装器allow_url_include = 0

Gag*_*ags 42 php include

我试图在页面中包含一个php文件

  require_once(http://localhost/web/a.php)
Run Code Online (Sandbox Code Playgroud)

我收到了一个错误

 Warning: require_once(): http:// wrapper is disabled in the server configuration by   allow_url_include=0
Run Code Online (Sandbox Code Playgroud)

我改变allow_url_include=1了php.ini并且有效,但我不认为每个人都会让我更改他们的php.ini文件.

那么,有什么方法可以实现这一目标吗?

Nad*_*han 60

生成警告是因为您正在使用包含的文件的完整URL.这不是正确的方法,因为这样您将从Web服务器获取一些HTML.使用:

require_once('../web/a.php');
Run Code Online (Sandbox Code Playgroud)

这样web服务器就可以执行脚本并提供输出,而不仅仅是提供源代码(当前导致警告的情况).


小智 19

尝试在我的Wordpress主题中包含一个PHP文件时,我遇到了同样的错误.通过引用文件名,我能够绕过它dirname(__FILE__).我无法使用相对路径,因为我的文件将被包含在整个主题的不同位置,所以类似的东西require_once '../path-to/my-file'不起作用.

更换require_once get_template_directory_uri() . '/path-to/my-file'require_once dirname( __FILE__ ) . '/path-to/my-file'的伎俩.

  • 如果您使用的是 WordPress,只需使用 get_template_directory() 而不是 get_template_directory_uri() 就可以了 (2认同)

小智 12

试着用

<?php require_once($_SERVER['DOCUMENT_ROOT'].'/web/a.php'); ?>
Run Code Online (Sandbox Code Playgroud)


Sof*_*ide 6

您必须将路径放到文件中.例如:

require_once('../web/a.php');
Run Code Online (Sandbox Code Playgroud)

你无法从互联网上获取文件(使用http协议)它受到限制.这些文件必须位于同一台服务器上.有可能看到对方(权利)

Dir-1 -
         > Folder-1 -> a.php
Dir-2 -
         > Folder-2 -> b.php

To include a.php inside b.php => require_once('../../Dir-1/Folder-1/a.php');
To include b.php inside a.php => require_once('../../Dir-2/Folder-2/b.php');
Run Code Online (Sandbox Code Playgroud)


zia*_*and 5

WORDPRESS 主要出现此错误:
解决方案:
在远程实时托管服务器或“本地服务器”上找到 PHP 安装目录(
如果是 Windows 操作系统,
例如,如果您使用 xampp 或 wamp 网络服务器)。它将位于 xammp 目录 'c:\xammp\php' 中
注意: 对于 Unix/Linux 操作系统,请在 Web 服务器中找到 PHP 目录

查找并编辑PHP.INI文件
查找'allow_url_include'
将其替换为值 '
on'allow_url_include=On
Save你php.ini重新启动你的网络服务器。