Page 1 of 1
Include/Filepath
Posted: Fri Dec 05, 2003 2:25 pm
by ptysell
Ok, so here is my problem. Essentiall i have a dynamic site that works perfectly except for one problem. I have a PHP file which is essentiall a placer file that is placed into the directory.
The reason i have a driectory structure the way it is, is not important.
Here is my problem:
I have my site set up in this fassion
home/assets/php/global/
home/assets/php/header/
home/about/index.php
home/about/mission/
and so on for different reasources.
Now i just want one file with nothing changed in each file for easy management.
i know that i have to traverse down a dir by ../../ depending on where the file is.
This creates a problem
I have tried a lot of things. Nothing has helped. Now i have gotten it to work with an absolute path but if i change the server then this does not help.
what i was thinking was that i could have the PHP file find its absolute path, subtract it relitive path, and then add the path to the desired php file, maby a golbal file which could contain all the other paths in ../.. form.
i dont know, maybe i am making this too complicated.
Thanks
Host specific config files
Posted: Fri Dec 05, 2003 2:51 pm
by dyconsulting
One idea is to create host specific config file. In this file you might store host specific db connection info and home path. This file (dev_config.php)you will never upload to you production. What you will upload to production is (prod_config.php).
You will then include this file on your pages. Contents of dev_config.php:
Code: Select all
<?php
$db = "db_name";
$user = "username";
$pass = "password";
$home_path = "http://localhost/";
?>
My file has a lot more stuff in it. I then (optional) use perl script to programmaticly create these config files based on which machine I am on. Local creates dev_config.php, stage creates stage_config.php and production -> prod_config.php
Posted: Fri Dec 05, 2003 4:47 pm
by ptysell
i am not sure if i understand you correctly. I have a file like that. what i am running into is that when itry to include that file via
include{lallalala) i need to have a path which is not relative but absolute. now i can not have the absolute path as the server sees it because if the server is changed then every file would need to be edited. I cant do ../../../ whatever because if the holder pages are on different levels then the about of ./.. would change.
Now i may be missing something in your soluition. I know when doing java programming, i could just install packages that contain class files into a certain folder allowing these files to be called without the include command.
I guess what i am asking, as i am new to this, is on a server isi thee a user confiurable folder where i can install files containing functionsthat they system will look at when i call a function that they contain.
Thanks
Posted: Fri Dec 05, 2003 6:35 pm
by dyconsulting
How about this. From your file, let's call it test.php you include config file:
Code: Select all
<?php
include (../includes/dev_config.php");
...
// Now this is relative to you test.php file and this realtionship will never change
?>
In dev_config.php file you provide variable called $home_path. This is what you will use in your test.php code to have that absolute path ie:
Code: Select all
<?php
include (../includes/dev_config.php");
include ($home_path ."/includes/other_files.php");
?>
If you ever need to change hosts, just change that variable to a new host.
One more thing. Absolute path is not so evil, how many times are you planning to change domain names?
Posted: Fri Dec 05, 2003 7:15 pm
by ptysell
that still requires me to have 3 different files. One for the index with
assets/php/global/, one for a section with ../assets/php/global/, and one for a subsection with ../../assets/php/global/. I do not know why i cant just reference it thought http://
one thing that i am thinking is that i could get the path relative to the host so that
http://www.vvv.com/xxx/yyy/zzz/index.php
would return
xxx/yyy/zzz/index.php
and then parse it at / and count the array.
I would then need to subtract one(for the index.php)
and loop an array though that number adding ../ for each.
then add that to the beginning of assets/php/global/whatever
I dont see why this would work.
Any comments?
Posted: Fri Dec 05, 2003 9:26 pm
by DuFF
Heres a start:
Code: Select all
<?php
$pathdir = $_SERVER['PHP_SELF'];
echo $pathdir; // will return: path/to/the/file.php
$patharray = explode("/", $pathdir);
print_r($patharray); // will return: ( [0] =>path [1] => to [2] =>the [3] =>file.php )
?>
Now just loop through the array while adding ../