Jump to content

Recommended Posts

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.

 

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';

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.

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.