PHP include - failed to open stream: No such file

2019-02-10 16:36发布

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.

Header file: /test/assets/header.php

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

Throws Error: /test/company/index.php contains <?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 disabled error

标签: php include
3条回答
\"骚年 ilove
2楼-- · 2019-02-10 16:42

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.

Commonly this is what was meant:

include "$_SERVER[DOCUMENT_ROOT]/test/assets/header.php";
// Note: array key quotes only absent in double quotes context
查看更多
虎瘦雄心在
3楼-- · 2019-02-10 16:47

It's not a URL; it's a filepath (although you can use full URLs when the server configuration allows it).

查看更多
趁早两清
4楼-- · 2019-02-10 16:54

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.

查看更多
登录 后发表回答