spryce Posted January 8, 2011 Share Posted January 8, 2011 My very first post..... please be gentle. Question 1: Why does this work? - include("./includes/constants.php"); Ok here goes - I have a website which was written in php. I did not write it and the person who did was new to php and programming - as am I. I have the job of completing it. It was written trying to follow an OO model. I have a class 'Database.php' which handles the DB connection side of things. Pretty standard stuff. This database class 'includes' a file called 'constants.php' which stores the DB_SERVER, DB_USER, DB_PASS and DB_NAME properties. HERE IS THE CATCH. The includes statement says: include("./includes/constants.php"); I dont understand the single dot and have not yet used it. The site works fine - with the exception that my forms do not work. The site pulls data, images, urls from the database so it obviously connects ok. The constants.php file is located up one level then in the includes folder. ie include("../includes/constants.php"); If I add a second dot ( ../ )it breaks - even though the path is correct with 2 dots. I dont get it. Question 2: So to add insult to injury when I leave it as a single dot (if it aint broke, dont fix it) and try to post a form I get these error messages: Warning: include(./includes/constants.php) [function.include]: failed to open stream: No such file or directory in /home/******/public_html/development/classes/database.php on line 2 Warning: include(./includes/constants.php) [function.include]: failed to open stream: No such file or directory in /home/******/public_html/development/classes/database.php on line 2 Warning: include() [function.include]: Failed opening './includes/constants.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/******/public_html/development/classes/database.php on line 2 I am a total newb at this but my english tells me that all of a sudden it is looking for my constants.php file in a total new path either :/usr/lib/php or usr/local/lib/php. Is there something special about 'includes'? I have used requires and require_once etc for other included files (in this website) without any path drama. I am using cPanel and I dont even see those directories - /usr/lib/php:/usr/local/lib/php. I hope I didnt put anyone to sleep. Both of these seem like simple problems, but I have tried and failed. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/223785-directory-issues-why-does-my-website-work/ Share on other sites More sharing options...
ignace Posted January 8, 2011 Share Posted January 8, 2011 The . (dot) refers to the directory the file is currently in, so include('./includes is the same as include('includes however it's always best to use absolute paths instead of relative. Assuming the directory is in the root directory: include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/constants.php'; Quote Link to comment https://forums.phpfreaks.com/topic/223785-directory-issues-why-does-my-website-work/#findComment-1156702 Share on other sites More sharing options...
spryce Posted January 8, 2011 Author Share Posted January 8, 2011 Awesome thanks. That solves my single dot issue ( and why ive never used it ) but since this is a relative path I still cant see why it works. The path to database.php is: development/classes/database.php - and the path to constants.php is: development/includes/constants.php so we arent in the same directory and I actually need to go up one level and into includes - or I need a week off because my brain has malfunctioned. Quote Link to comment https://forums.phpfreaks.com/topic/223785-directory-issues-why-does-my-website-work/#findComment-1156718 Share on other sites More sharing options...
ignace Posted January 8, 2011 Share Posted January 8, 2011 Awesome thanks. That solves my single dot issue ( and why ive never used it ) but since this is a relative path I still cant see why it works. The path to database.php is: development/classes/database.php - and the path to constants.php is: development/includes/constants.php so we arent in the same directory and I actually need to go up one level and into includes - or I need a week off because my brain has malfunctioned. That's because the path is relative to the script that called it. So if index.php includes Database then the database class has to include constants relative from index.php hence why it's desired to use an absolute path when working with multiple calling scripts. Quote Link to comment https://forums.phpfreaks.com/topic/223785-directory-issues-why-does-my-website-work/#findComment-1156739 Share on other sites More sharing options...
spryce Posted January 9, 2011 Author Share Posted January 9, 2011 Ahhhhhh....... and the penny drops. I will go and change the paths to absolute and see if that fixes my other problem as well. Thanks man Quote Link to comment https://forums.phpfreaks.com/topic/223785-directory-issues-why-does-my-website-work/#findComment-1156919 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.