michaellindahl
michaellindahl

Reputation: 2052

PHP include - failed to open stream: No such file

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

Upvotes: 6

Views: 78661

Answers (3)

Ramiz Syed
Ramiz Syed

Reputation: 51

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.

Upvotes: 5

mario
mario

Reputation: 145512

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

Upvotes: 16

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799430

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

Upvotes: 0

Related Questions