Jump to content

How to maintain correct path with includes


garry

Recommended Posts

So I'm sort of planning for the future here...

 

At the moment, I'm creating a website and its address is http://localhost/dev

 

When I use includes for the config and function files, i'm currently using this

 

include_once ($_SERVER['DOCUMENT_ROOT'] . "/dev/includes/whatever.php");

 

I know that once I move the site to a different server, everything will not display because I've added /dev/ to the paths. Can anyone suggest a way to fix this? I've tried making a variable in the config.php but it means I have to set the variable at the top of each page for it to include the config file. Can anyone please help?

Link to comment
Share on other sites

$doc_root = getcwd();

 

That will return the full path to your working directory. So running...

/var/www/dev/includes/whatever.php

 

$doc_root == '/var/www/dev/includes/'

 

but if say your were running...

/var/www/dev/index.php

 

$doc_root == '/var/www/dev/'

 

now you should define doc_root as a constant

define('DOC_ROOT', $doc_root);

 

then including files would be something like:

include DOC_ROOT . 'includes/whatever.php';

 

hope that helps

Link to comment
Share on other sites

$doc_root = getcwd();

 

That will return the full path to your working directory. So running...

/var/www/dev/includes/whatever.php

 

$doc_root == '/var/www/dev/includes/'

 

but if say your were running...

/var/www/dev/index.php

 

$doc_root == '/var/www/dev/'

 

now you should define doc_root as a constant

define('DOC_ROOT', $doc_root);

 

then including files would be something like:

include DOC_ROOT . 'includes/whatever.php';

 

hope that helps

 

Thanks for your help!

 

I tried getcwd() function works well and it all works great when I define the DOC_ROOT in the document i'm working on but whenever I try to use it in a different document I get an undefined error. Do you know how I can fix this?

 

I considered relative paths but I thought that they could get complicated down the track as more and more files are added so it would be easier to simply use the absolute paths.

Link to comment
Share on other sites

The only reason I wanted to do absolute was including files with paths in them. For example, if I had a file "includes/includes.php" that contained a link to "../file.php" and then I wanted to include that in the index.php AND the "style/style.php" file. Wouldn't it not work because the link would only be relative to one of the files? (Sorry if i'm explaining this bad).

Link to comment
Share on other sites

Ok, I'm not sure, but it sound's like you've run into some sort of scope issue.

 

The way I usually setup my projects is something like this.

 

request goes to index.php, or any other request file in my doc_root

 

then, all request files require some sort of sessions file, or config file that starts the session, sets up db connections, and establishes the doc_root using getcwd()

 

If all that is set in main scope...ie not in a function, or class method, those vars will available anywhere in your includes except when they lose scope to the inside of a function or class method. Constants like DOC_ROOT however ARE available in the global scope so it may be used wherever you'd like.

 

If this doesn't come close, please leave a more detailed description, or post more code so that I can debug it for you on my local server here.

Link to comment
Share on other sites

I'm with you garry.

 

I setup my rootpath, then I define contants such as:

ROOT_PATH

INC_PATH

CLASS_PATH

UPLOAD_PATH

 

those are probably the most frequently used for me. But I don't like messing with php's pointer's either. Sometimes they just don't seem to know what your talking about.

Link to comment
Share on other sites

Okay so I'm trying to do what you suggested but am having trouble with the relative paths.. it just doesn't seem to be working. Here's my folder structure

 

index.php

config.php

includes/includes.php

includes/functions.php

 

I'm using this code in "includes.php"

 


<?php

require("config.php"); 

include ('functions.php');


?>

 

And this code in index.php

 

<?php

$title = "Home";

require_once('config.php');
require_once('includes/includes.php');

?>

 

But I keep getting this error:

 

 

Warning: require(../config.php) [function.require]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/dev/includes/includes.php on line 4

 

Fatal error: require() [function.require]: Failed opening required '../config.php' (include_path='.:/Applications/MAMP/bin/php5/lib/php') in /Applications/MAMP/htdocs/dev/includes/includes.php on line 4

Link to comment
Share on other sites

Using the code you gave me I get another error :(

 

 

Warning: require(../config.php) [function.require]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/dev/includes/includes.php on line 5

 

Fatal error: require() [function.require]: Failed opening required '../config.php' (include_path='.:/Applications/MAMP/bin/php5/lib/php') in /Applications/MAMP/htdocs/dev/includes/includes.php on line 5

Link to comment
Share on other sites

Using the code you gave me I get another error :(

 

 

Warning: require(../config.php) [function.require]: failed to open stream: No such file or directory in /Applications/MAMP/htdocs/dev/includes/includes.php on line 5

 

Fatal error: require() [function.require]: Failed opening required '../config.php' (include_path='.:/Applications/MAMP/bin/php5/lib/php') in /Applications/MAMP/htdocs/dev/includes/includes.php on line 5

 

Usually the problem is small and simple, check all your directory's and filenames over. Check for proper syntax in your coding and make sure all the pieces fit togeather. The error is either telling you the filename does not exist, or you have missed a charactor in your syntax. Double back and double check fix's about 80% of the problems.

Link to comment
Share on other sites

It definitely does exist.

 

Okay, i changed some stuff and it seems to be working now.

 

index.php:

<?php

$title = "Home";

require ("includes/includes.php");
require ("config.php");
?>

 

includes/includes.php:

<?php

include ("functions.php");


?>

 

Link to comment
Share on other sites

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.