PHP include - could not open stream: no such file

I wrote the website in HTML, but then I wanted the header and part of the navigator to be in the same file, so I just needed to change it once. I changed all the pages to .php and added php. However, now I get an error on the pages that I put in folders, so I could create messy links.

Header file: /test/assets/header.php

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

Throws an error: /test/company/index.php contains <?php include '/test/assets/header.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 a problem with a header file located in a folder in the root folder. I believe this is a simple problem, just not knowing how to enter the URL. If I try to include the full path to header.php, I get a URL file-access is disabled error URL file-access is disabled

+4
source share
3 answers

If you start the path with / , then this is the absolute path. The absolute network is the real root directory of the file system, not the virtual document root . That is why he fails.

This is usually what it meant:

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

I also came across 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 must define your path and then use the include function as shown below:

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

After that you can use any directory structure.

+4
source

This is not a URL; this is the path to the file (although you can use full URLs when server configuration permits this).

0
source

Source: https://habr.com/ru/post/1388746/


All Articles