Require_once fails for unknown reason

Basic hierarchy:
-- example1.php
item.php

example1.php has simple:
require_once("../item.php");

Typical error:
Warning: require_once(../item.php): failed to open stream: No such file or directory in /opt/lampp/htdocs/sub/sub/example1.php on line 3

Fatal error: require_once(): Failed opening required '../item.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/sub/sub/example1.php on line 3

Yes, that file is there. It’s just one layer above, no finnicky stuff.
The permissions are -rw-rw-r--, triple read.
Require command is used in first line of actual code.

What did I do?

I’m not sure that the ../ syntax works in php, I think it’s just an html thing.
Try using $_SERVER["DOCUMENT_ROOT"] .' /restOfPath/' or the full path from the root.

1 Like

Well that solved about 30 issues at once.

JFYI ../ syntax works for PHP, C++, Bash, Linux, Windows, Mac and probably any other computer system in the world that works with filesystem-like hierarchy.

The only problem with it is that it’s being a relative address, and thus less reliable than absolute path you suggested.

1 Like

Here’s the working one:

require_once(__DIR__."/../pathto.php");

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.