Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP include - failed to open stream: No such file

Tags:

include

php

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

like image 906
michaellindahl Avatar asked Dec 31 '11 17:12

michaellindahl


1 Answers

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
like image 125
mario Avatar answered Oct 02 '22 10:10

mario