Page 1 of 1

[SOLVED]file include paths

Posted: Sun Oct 02, 2005 6:34 pm
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?

Posted: Sun Oct 02, 2005 6:51 pm
by feyd

Posted: Sun Oct 02, 2005 6:58 pm
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.

Posted: Sun Oct 02, 2005 7:11 pm
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

Posted: Mon Oct 03, 2005 5:43 pm
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.