garry Posted May 10, 2008 Share Posted May 10, 2008 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? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 10, 2008 Share Posted May 10, 2008 Just put them in the same folder (or down in a directory) and use relative paths. o-O Quote Link to comment Share on other sites More sharing options...
mlin Posted May 10, 2008 Share Posted May 10, 2008 $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 Quote Link to comment Share on other sites More sharing options...
dezkit Posted May 10, 2008 Share Posted May 10, 2008 hey, is your last name "newman" ? Quote Link to comment Share on other sites More sharing options...
mlin Posted May 10, 2008 Share Posted May 10, 2008 nope, my last name is Linardy. why do you ask? http://linardy.com/ Quote Link to comment Share on other sites More sharing options...
garry Posted May 10, 2008 Author Share Posted May 10, 2008 hey, is your last name "newman" ? nope. Quote Link to comment Share on other sites More sharing options...
garry Posted May 10, 2008 Author Share Posted May 10, 2008 $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. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 10, 2008 Share Posted May 10, 2008 Relative paths are the way to go. That means it'll work on any server that maintains your folder structure. Otherwise, you'll need to keep messing around with absolute paths. Quote Link to comment Share on other sites More sharing options...
garry Posted May 10, 2008 Author Share Posted May 10, 2008 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). Quote Link to comment Share on other sites More sharing options...
mlin Posted May 10, 2008 Share Posted May 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
mlin Posted May 11, 2008 Share Posted May 11, 2008 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. Quote Link to comment Share on other sites More sharing options...
garry Posted May 11, 2008 Author Share Posted May 11, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 11, 2008 Share Posted May 11, 2008 Relative to the individual file. includes/includes.php: <?php require("../config.php"); include ("functions.php"); ?> index.php: <?php require ("includes/includes.php"); ?> SO the way to go. =P Quote Link to comment Share on other sites More sharing options...
garry Posted May 11, 2008 Author Share Posted May 11, 2008 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 Quote Link to comment Share on other sites More sharing options...
DarkWater Posted May 11, 2008 Share Posted May 11, 2008 Then config.php doesn't exist. Quote Link to comment Share on other sites More sharing options...
rhyboo Posted May 11, 2008 Share Posted May 11, 2008 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. Quote Link to comment Share on other sites More sharing options...
garry Posted May 11, 2008 Author Share Posted May 11, 2008 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"); ?> Quote Link to comment Share on other sites More sharing options...
rhyboo Posted May 11, 2008 Share Posted May 11, 2008 Okay sweet, thats good to hear. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.