Search code examples
phpdirectorypathrequire

PHP Warning: require(...): Failed to open stream: No such file or directory in


I´m working on a onlineshop.

I need to require the "nav_bar.php" file into the "item_page.php" file.

Here´s my directory:

directory

And here´s my code:

require "../nav_bar/nav_bar.php";

note: I also tried a lot of other "path" variations but it never worked.

Some examples of my tries:

require "source\main\nav_bar\nav_bar.php";

require "C:\Users\holli\OneDrive\Dokumente\onlineshop\source\main\nav_bar\nav_bar.php";

A few days ago, I got this Error. I already searched a ton of other questions and a lot of other websites with this error, but I can not find a answer for my problem...

I know, that this error is somehow common but I don´t know how I can solve this one.

Can anyone help me?

Here´s the error message:

error: Command failed: "C:\xampp\php\php.exe" "c:\Users\holli\OneDrive\Dokumente\onlineshop\source\main\item_page\item_page.php" PHP Warning:  require(../nav_bar/nav_bar.php): Failed to open stream: No such file or directory in C:\Users\holli\OneDrive\Dokumente\onlineshop\source\main\item_page\item_page.php on line 8 PHP Fatal error:  Uncaught Error: Failed opening required '../nav_bar/nav_bar.php' (include_path='C:\xampp\php\PEAR') in C:\Users\holli\OneDrive\Dokumente\onlineshop\source\main\item_page\item_page.php:8 Stack trace:
#0 {main}   thrown in C:\Users\holli\OneDrive\Dokumente\onlineshop\source\main\item_page\item_page.php on line 8

Error message

note: I use a VS-Code extension named: "five server"

But I don´t think, this extension has something to do with the error.

Heres the extension:

https://marketplace.visualstudio.com/items?itemName=yandeu.five-server

Thank you in advance :)


Solution

  • The include_path does not include the current directory...

    Heres a example to solve this, with an absolute path:

    require dirname(__DIR__)."/nav_bar/nav_bar.php";
    

    Thanks @Kevin Y for the comment that solved that question