首页 文章

PHP包括 - 无法打开流:没有这样的文件

提问于
浏览
4

我用HTML写了一个网站,但后来我想把 Headers 和导航栏部分放在一个文件中,所以我只需要改一次 . 我将所有页面都更改为.php并添加了php包含 . 但是现在我在我放在文件夹中的页面上出现错误,所以我可以创建无瑕疵的链接 .

头文件: /test/assets/header.php

作品: /test/index.php 包含 <?php include 'assets/header.php'; ?>

引发错误: /test/company/index.php 包含 <?php include '/test/assets/header.php'; ?>

错误:

Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory

Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')

我有一个问题链接到根文件夹中的文件夹中的头文件 . 我认为这是一个简单的问题,只是不知道如何输入URL . 如果我尝试包含header.php的完整路径,我会收到 URL file-access is disabled 错误

3 回答

  • 4

    这不是一个URL;它是一个文件路径(虽然您可以在服务器配置允许时使用完整的URL) .

  • 0

    我也面临同样的警告:

    Warning: include(/test/assets/header.php) [function.include]: failed to open stream: No such file or directory
    Warning: include() [function.include]: Failed opening '/test/assets/header.php' for inclusion (include_path='.:/usr/local/lib/php:/usr/local/php5/lib/pear')
    

    我用以下解决方案解决了它们:

    你必须定义你的路径,然后使用include函数如下:

    define('DOC_ROOT_PATH', $_SERVER['DOCUMENT_ROOT'].'/');
    require DOC_ROOT_PATH . "../includes/pagedresults.php";
    

    之后,您可以使用任何目录结构 .

  • 11

    如果你用 / 开始一个路径,那么它就是一个绝对路径 . 绝对主管到实际的文件系统根目录,而不是虚拟document root . 这就是它失败的原因 .

    通常这就是:

    include "$_SERVER[DOCUMENT_ROOT]/test/assets/header.php";
    // Note: array key quotes only absent in double quotes context
    

相关问题