Jump to content

A little problem regarding include files from another directory


Go to solution Solved by helloworld001,

Recommended Posts

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.

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 by VanityCrush

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

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 by VanityCrush

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?

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 by VanityCrush

 

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?

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.

  • Solution

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');

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.