【问题标题】:Warning: require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0警告:require_once(): http:// wrapper 在服务器配置中被 allow_url_include=0 禁用
【发布时间】:2014-06-10 17:14:33
【问题描述】:

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

  require_once(http://localhost/web/a.php)

我收到一个错误

 Warning: require_once(): http:// wrapper is disabled in the server configuration by   allow_url_include=0

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

那么,有没有办法做到这一点?

【问题讨论】:

    标签: php include


    【解决方案1】:

    您必须输入文件的路径。例如:

    require_once('../web/a.php');
    

    您无法从 Internet(使用 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');
    

    【讨论】:

    • 我知道.. 但是如果我想在两个目录中包含相同的文件.. 像文件 a.php 放在 DIR 1 中,现在我想将它包含在 DIR 3 文件中..并且一个具有来自 dir 3 的 a.php 的文件包含在 dir 1 文件之一中......所以它给出了错误 a.php 不存在
    • 请告诉我目录结构。没有它,我很难提供帮助。
    • 它是目录 1 --> 文件夹 1 --> a.php 和目录 2 --> 文件夹 2 --> b.php [它有 require_once('../../a .php')] .. 现在,当我在目录 1 中的一个文件中包含 b.php 时,它会抛出一个错误,即 a.php 不存在,根据目录结构,这是正确的
    • 我已经编辑了我之前的答案,因为这里没有更好的“视图”的帖子结构。
    【解决方案2】:

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

    require_once('../web/a.php');
    

    以便网络服务器可以执行脚本并提供其输出,而不仅仅是提供源代码(您当前的情况会导致警告)。

    【讨论】:

      【解决方案3】:

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

      require_once dirname( __FILE__ ) . '/path-to/my-file' 替换require_once get_template_directory_uri() . '/path-to/my-file' 就可以了。

      【讨论】:

      • 这可能适用于 require_once 可以稍后评估的情况,因为它似乎不是在预编译阶段,而是在函数评估阶段的后期。如果您需要在需要另一个文件而不使用 dirname() 调用(例如在父主题中)之前需要该文件,它会阻止您的文件首先被评估的任何机会......至少这似乎是 WordPress 服务器的情况。
      • 如果您使用的是 WordPress,只需使用 get_template_directory() 而不是 get_template_directory_uri() 就可以了
      【解决方案4】:

      尝试使用

      <?php require_once($_SERVER['DOCUMENT_ROOT'].'/web/a.php'); ?>
      

      【讨论】:

      • 很好的解决方案,避免了上面似乎无法规避的一些烦人的 Wordpress 错误
      【解决方案5】:
      require_once('../web/a.php');
      

      如果这对任何人都不起作用,以下是在项目中的任何位置包含文件的好主意。

      require_once dirname(__FILE__)."/../../includes/enter.php";
      

      此代码将从当前目录之外的 2 个目录中获取文件。

      【讨论】:

        【解决方案6】:
        require_once(APPPATH.'web/a.php');
        

        在 codeigniter 中为我工作

        检查reference

        【讨论】:

          【解决方案7】:

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

          查找和编辑 PHP.INI 文件
          查找 'allow_url_include'
          将其替换为值 'on'
          allow_url_include=On
          为您保存 php.iniRESTART 您的网络服务器。

          【讨论】:

            【解决方案8】:

            echo file_get_contents('http://localhost/web/a.php'); //最好的例子

            【讨论】:

              【解决方案9】:

              include 和 require 函数需要 文件路径,但您提供的是 文件 URI。该参数不应该是包含http/https的参数。

              你的代码应该是这样的:

              include ("folder/file.php");
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2017-11-24
                • 2019-03-06
                • 2011-10-16
                • 1970-01-01
                • 2017-08-14
                • 2018-01-22
                相关资源
                最近更新 更多