[SOLVED]file include paths

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

[SOLVED]file include paths

Post by blacksnday »

yes this problem is not a rare one :P

I am trying to figure out a way to provide includes that
will work on any directory structure, that will also allow
the path to be defined in one location.

The problem I have been having, is when trying to use it in different ways.
Server paths is always not a recommended way of doing things, but so far its
the only way all options have worked, yet still not viable because then it cant be
defined from just one file.

If trying to include my shoutbox from my server Root index.php then all the
php include paths from the shoutbox files need to be changed to reflect this and work,
which will result in it not working if you go directly to the shoutbox folder.
and the problems continue from there...

My question would be how can you create a way for files inside certain folders that
will always stay there.... work correctly if including to another file in different level directory, and/or
if using through an iframe, and/or if directly accessing?
Last edited by blacksnday on Mon Oct 03, 2005 5:44 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

I was checking the net and came across this

Code: Select all

$http_dir="YOUR.DOMAIN.COM"; 
// GET URL_ROOT 
if(isset($_SESSION["url_root"])) //CHECK IF VAR EXIST 
{$url_root=$_SESSION["url_root"];} else {$url_root="http://".$_SERVER['HTTP_HOST']."/".$http_dir.""; 
session_register("url_root"); $_SESSION["url_root"]=$url_root;} 
//usage example
echo "<a href='$url_root/images/blah/blah/page.php'>";
this is nice and does what I need.
However, if i put this in ... lets say, my config.php file which is included by another that
then spreads it out for rest of script to use... well that still leaves the
php include problem since it cant read a variable until it has the file included, yet
the variable is what defines the structure... hmm....

basically all i need to figure out is how to
include a file in such a way that it will be able to be accessed correctly no matter
what the folder structure relation is to the included file.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

I think i may have found a solution.

Code: Select all

if (!defined( "DIR_PATH" )) {
        define( "DIR_PATH", dirname(__FILE__)."/");
    }
//example usage
include_once( DIR_PATH."path/to/file.php" );
using that in my index then using the example usage in all files should fix the problem
i think. gonna go try it out now
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Ok just to update... using

Code: Select all

if (!defined( "DIR_PATH" )) {
        define( "DIR_PATH", dirname(__FILE__)."/");
    }
in my index.php to get the directory path known,
then using variables in the config file to state the file paths
once the define read the file, solved my problems perfectly.

The problem came from my shoutbox and me trying to make the entire shoutbox folder
contents readable no matter how it was being used...
Either by IFRAMES or php Includes or by Direct access.
Post Reply