0

It's normal that I get a PHP warning trying to include_once the same file twice.

The first include_once properly loads the PHP file. The second include_once with the same file obviously gives me an error.

My question is why am I getting a "Warning: include_once(../../config.php): failed to open stream: No such file or directory in ..." and a "Warning: include_once(): Failed opening ..." when the file clearly exists.

I'm just curious as to why PHP wouldn't tell me something like "file already included" or something, but instead says the file doesn't exist.

My structure: I have a page with internal navigation using jQuery .load().

The initial page is loaded like this:

<?php
include_once("header.php");
if ($sessid === $idn) include_once("components/activity_feed.php");
else { include_once("components/my_textbooks.php");}
include_once("footer.php"); 
?>

header.php contains an include_once to ../config.php for server conf.

components/activity_feed.php also includes an include_once to ../../config.php, so when components/my_textbooks.php is requested via .load() the config file is loaded and the page is rendered before being served.

Michał
  • 868
  • 1
  • 10
  • 36
  • Where is the file located you are trying to `include_once()` and where is the file with `include_once()`? – Tom Udding Mar 26 '17 at 13:10
  • The directory exists and the file is properly included the first time. The error above only shows when doing an include_once again on the same file in the same PHP page. – Michał Mar 26 '17 at 13:12
  • PHP doesn't throw a "file not found" if you include the same file twice with `include_once`. It simply returns `true` if you include it again. There must be something else causing this. Are the includes in the same file? If not, are they located in the same folder? Can you tell us the folder structure and where these files are located, and how the files are included/related? – M. Eriksson Mar 26 '17 at 13:20
  • Hey @MagnusEriksson! Just updated the question with some more code... – Michał Mar 26 '17 at 13:32
  • When including files that includes files etc, you should use absolute paths, not relative. Relative paths will be relative from the main script, not the including file. Check out the post in my previous comment. – M. Eriksson Mar 26 '17 at 13:38
  • Yup, that was it! If you'd like to post it as an answer I'd gladly mark it. – Michał Mar 26 '17 at 13:57

0 Answers0