Jump to content

Recommended Posts

Hey all,

I've recently encountered a pesky bug I've been unable to really fix.

First off, here's a picture showing roughly how my site is (FTP, blanked out directories that are irrelevant to my issue):

Screen_Shot_2013_02_13_at_8_12_45_PM.png

Now, I have another directory in the /essentials/ folder, called /includes/, where I have 2 files, called header.php and footer.php.

Within header.php, I have this code:

<?php
chdir("community/");
require_once('./global.php');
?>

Now, I have a file in the /account/ folder called index.php. It contains this code:

include_once($_SERVER['DOCUMENT_ROOT'].'/essentials/includes/header.php');
include_once($_SERVER['DOCUMENT_ROOT'].'/essentials/includes/footer.php');

I've tried using chdir in this file, yet it doesn't work!

Anyways, this should function correctly. Yet, I encounter an odd issue when I execute this code.

 

Error:

Warning: chdir() [function.chdir]: No such file or directory (errno 2) in /home/208/public_html/essentials/includes/header.php on line 5

Warning: require_once(./global.php) [function.require-once]: failed to open stream: No such file or directory in /home/208/public_html/essentials/includes/header.php on line 6

Fatal error: require_once() [function.require]: Failed opening required './global.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/208/public_html/essentials/includes/header.php on line 6

 

So, for some reason, I'm having issues. If my code in /account/index.php includes header/footer correctly, I get errors in my header.php file. If it doesn't include the header/footer files, then I get another issue, which says:

Warning: include_once(/home/208/public_html/account/essentials/includes/header.php) [function.include-once]: failed to open stream: No such file or directory in /home/208/public_html/account/index.php on line 3

Warning: include_once() [function.include]: Failed opening '/home/208/public_html/account/essentials/includes/header.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/208/public_html/account/index.php on line 3

Warning: include_once(/home/208/public_html/account/essentials/includes/footer.php) [function.include-once]: failed to open stream: No such file or directory in /home/208/public_html/account/index.php on line 4

Warning: include_once() [function.include]function.include]: Failed opening '/home/208/public_html/account/essentials/includes/footer.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/208/public_html/account/index.php on line 4

 

So, my question is: What would be the correct way to fix this issue? Would I need to change the contents of the header.php file, the index.php file, or both?

 

 

Thanks for all help,

Mark

Edited by Mko
Link to comment
https://forums.phpfreaks.com/topic/274464-chdir-issue-including-files/
Share on other sites

The current working directory for your scripts is going to be whatever directory the initial entry point file is; in your case /home/208/public_html/account/  All your relative paths are going to be based on this directory.

 

When you try and do chdir() in your header.php file you're asking to move into the community folder relative to the current directory.  Since the current directory is /home/208/public_html/account/, it will attempt to change to /home/208/public_html/account/community/, hence the no such file/directory error.

 

You shouldn't really be messing with chdir anyway, there is generally no need to.  Also you should attempt to use absolute paths as much as possible so it makes things clear where items are located.  Note that using an absolute path doesn't mean hard-coding the path.  There are plenty of constants/variables to use to determine the full path to a file.

 

For example in your header.php, to include global.php you could use:

require_once __DIR__.'/global.php';

 

__DIR__ is a magic constant which refers to the directory that the currently executing file is stored in. Since at the point of that require statement the currently executing file is header.php, __DIR__ will be defined as /home/208/public_html/essentials/includes.  Note that __DIR__ requires php 5.3 or better, for lower versions you can use dirname(__FILE__) instead.

 

As you already seem to know, there is also $_SERVER['DOCUMENT_ROOT'] which points to the root directory of your site.  You can use that to generate a path to a file also.

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.