helloworld001 Posted August 23, 2015 Share Posted August 23, 2015 I am trying to include a file from another directory and so far I keep getting errors such as "failed to open stream: No such file or directory in...". Here's my directory setup. home/ /template/header.php /members/private/settings.php All I want to do is include "header.php" file on my "settings.php" page. Here's an example. settings.php <?php $dir = dirname(__FILE__); require_once $dir.'/template/header.php'; Can you tell me what's wrong with it? It's not working. Quote Link to comment Share on other sites More sharing options...
VanityCrush Posted August 23, 2015 Share Posted August 23, 2015 Can you not just use require_once 'template/header.php'; Also, where is the file calling this located? If it's in home/ it should work Quote Link to comment Share on other sites More sharing options...
VanityCrush Posted August 23, 2015 Share Posted August 23, 2015 (edited) Can't edit my previous answer, sorry for the double post. You could try: settings.php file require_once('template/header.php'); index.php file include('members/private/settings.php'); If you're calling it from the home/ dir With the $dir variable it would look like this: settings.php require_once($dir.'/template/header.php'); index.php file include($dir.'/members/private/settings.php'); You can drop the parenthesis, I just like to put them there. Edited August 23, 2015 by VanityCrush Quote Link to comment Share on other sites More sharing options...
helloworld001 Posted August 23, 2015 Author Share Posted August 23, 2015 Can't edit my previous answer, sorry for the double post. You could try: settings.php file require_once('template/header.php'); index.php file include('members/private/settings.php'); If you're calling it from the home/ dir With the $dir variable it would look like this: settings.php require_once($dir.'/template/header.php'); index.php file include($dir.'/members/private/settings.php'); You can drop the parenthesis, I just like to put them there. None of the examples you provided work. This is the type of error I get. Warning: require_once(C:\xampp\htdocs\other\members\private/template/header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\other\members\private\settings.php on line 3 Here's a better look at the directory structure. home(main directory) /template /header.php /members /private /settings.php Quote Link to comment Share on other sites More sharing options...
VanityCrush Posted August 23, 2015 Share Posted August 23, 2015 (edited) Try this.. require_once '../../template/header.php'; I don't think you can use the $dir variable here because it will always point to the /members/private/ path, while you need to go back two directories. The code written before works for me, having the same structure as you. But I am including it in an index.php file located in the /home dir so maybe that's why it didn't work for you.. Edited August 23, 2015 by VanityCrush Quote Link to comment Share on other sites More sharing options...
helloworld001 Posted August 23, 2015 Author Share Posted August 23, 2015 Try this.. require_once '../../template/header.php'; I don't think you can use the $dir variable here because it will always point to the /members/private/ path, while you need to go back two directories. The code written before works for me, having the same structure as you. But I am including it in an index.php file located in the /home dir so maybe that's why it didn't work for you.. Still doesn't work. I get this error. Warning: require_once(core/init.php): failed to open stream: No such file or directory in C:\xampp\htdocs\other\template\header.php on line 2 Fatal error: require_once(): Failed opening required 'core/init.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\other\template\header.php on line 2 I just noticed something. In my header.php file, I also require another file. Like this. header.php require_once 'core/init.php'; Here's the updated directory with the core folder. home(main directory) /core /init.php /template /header.php /members /private /settings.php So perhaps it's not working because I am calling 2 files from 2 different locations at the same time? Quote Link to comment Share on other sites More sharing options...
VanityCrush Posted August 23, 2015 Share Posted August 23, 2015 (edited) You could try including the init.php file in your settings.php file like so: require_once('../../template/header.php'); require_once('../../core/init.php'); And remove the call from the header.php file. Any reason why you're calling the init.php file from header.php? Or you can just have everything in your init.php file (the db connection, functions, and templates) which you can then include in your files. /home /core /init.php /dbcon /connection.php in your init.php file require('dbcon/db_connection.php'); require('../template/header.php'); in your setings.php file include('../../core/init.php'); Edited August 23, 2015 by VanityCrush Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 23, 2015 Share Posted August 23, 2015 Try changing your path to be '../core/init.php'; Quote Link to comment Share on other sites More sharing options...
helloworld001 Posted August 23, 2015 Author Share Posted August 23, 2015 You could try including the init.php file in your settings.php file like so: require_once('../../template/header.php'); require_once('../../core/init.php'); And remove the call from the header.php file. Any reason why you're calling the init.php file from header.php? Or you can just have everything in your init.php file (the db connection, functions, and templates) which you can then include in your files. /home /core /init.php /dbcon /connection.php in your init.php file require('dbcon/db_connection.php'); require('../template/header.php'); in your setings.php file include('../../core/init.php'); Yes that does work. Now one other thing that I've noticed. I have couple more files that I "require_once" in the "init.php" file and as well as include some snippets in the header. This poses an issue. If I make the file paths so that they show up and work properly for the files in "members" folder, those same file paths won't work for the files in the "home" directory. There has to be a cleaner way to do this no? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted August 23, 2015 Share Posted August 23, 2015 these things you are including/requiring are all relative to the document root folder. use $_SERVER['DOCUMENT_ROOT'] to form an absolute file system path to them - require $_SERVER['DOCUMENT_ROOT'] . /'core/init.php'; you also need to be consistent, just use require everywhere. Quote Link to comment Share on other sites More sharing options...
Solution helloworld001 Posted August 23, 2015 Author Solution Share Posted August 23, 2015 these things you are including/requiring are all relative to the document root folder. use $_SERVER['DOCUMENT_ROOT'] to form an absolute file system path to them - require $_SERVER['DOCUMENT_ROOT'] . /'core/init.php'; you also need to be consistent, just use require everywhere. You are right. I have it working finally. Though there is a slight mistake in your code. The "home" directory has to be included for it to work. Here is the updated line. It'll work, no matter which sub folder you are calling it from. require_once ($_SERVER['DOCUMENT_ROOT'] . '/home/core/init.php'); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.