Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does include path resolution work in require_once?
    text
    copied!<p>I was writing an web app in PHP, when I encountered a strange situation. To illustrate my problem, consider a web app of this structure:</p> <pre><code>/ index.php f1/ f1.php f2/ f2.php </code></pre> <p>Contents of these files:</p> <p>index.php:</p> <pre><code>&lt;?php require_once("f1/f1.php"); ?&gt; </code></pre> <p>f1.php:</p> <pre><code>&lt;?php require_once("../f2/f2.php"); ?&gt; </code></pre> <p>f2.php: blank</p> <p>now when I try to open index.php in my browser I get this error:</p> <pre><code>Warning: require_once(../f2/f2.php) [function.require-once]: failed to open stream: No such file or directory in /var/www/reqtest/f1/f1.php on line 2 Fatal error: require_once() [function.require]: Failed opening required '../f2/f2.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/reqtest/f1/f1.php on line 2 </code></pre> <p>Is there something obvious I'm missing? how do include paths work in PHP?</p> <hr> <p>Before I asked this question, I attempted to experiment and find out. I set up another test, like so:</p> <pre><code>/ index.php f1/ f1.php f2.php </code></pre> <p>index.php:</p> <pre><code>&lt;?php require_once("f1/f1.php"); ?&gt; </code></pre> <p>f1.php:</p> <pre><code>&lt;?php require_once("f2.php"); ?&gt; </code></pre> <p>f2.php: blank</p> <p>To my surprise (and utter confusion), this worked out fine!</p> <p>So, what is the secret behind the path resolution?</p> <p>PS I saw <a href="https://stackoverflow.com/questions/1966533/">this question</a>, but it still does not answer the second case that I've stated here.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload