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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8689577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:16:06  来源:igfitidea点击:

PHP include - failed to open stream: No such file

phpinclude

提问by michaellindahl

I wrote a website in HTML but then I wanted the header and navbar portion in one file so I just had to change it once. I changed all the pages to .php and added the php includes. However now I get an error on pages that I've put in folders so I could create cruftless links.

我用 HTML 编写了一个网站,但后来我想将标题和导航栏部分放在一个文件中,所以我只需要更改一次。我将所有页面更改为 .php 并添加了 php 包含。但是,现在我在放入文件夹的页面上遇到错误,因此我可以创建无垃圾链接。

Header file: /test/assets/header.php

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

Works: /test/index.phpcontains <?php include 'assets/header.php'; ?>

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

Throws Error: /test/company/index.phpcontains <?php include '/test/assets/header.php'; ?>

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

Error:

错误:

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')

I am having an issue linking to the header file that is in a folder in the root folder. I believe it is a simple issue of just not knowing how to input the URL. If I attempt to include the full path to the header.php I get a URL file-access is disablederror

我在链接到根文件夹中某个文件夹中的头文件时遇到问题。我相信这是一个简单的问题,就是不知道如何输入 URL。如果我尝试包含 header.php 的完整路径,则会出现URL file-access is disabled错误

回答by mario

If you start a path with a /then it's an absolute path. Absolute mains to the actual filesystem root directory, not the virtual document root. This is why it fails.

如果您以 a 开头的路径,/则它是绝对路径。对实际文件系统根目录的绝对主目录,而不是虚拟document root. 这就是它失败的原因。

Commonly this is what was meant:

通常这是什么意思:

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

回答by Ramiz Syed

I was also facing the same warnings:

我也面临同样的警告:

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')

I solved them with the following solution:

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

You have to define your path then use include function as below:

您必须定义您的路径,然后使用如下包含功能:

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

After that, you can use any directory structure.

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

回答by Ignacio Vazquez-Abrams

It's nota URL; it's a filepath (although you canuse fullURLs when the server configuration allows it).

不是一个 URL;它是一个文件路径(尽管您可以在服务器配置允许的情况下使用完整的URL)。