/    Sign up×
Community /Pin to ProfileBookmark

Accessing Root Directory with php include?

I am trying to access my root directory “include” files, however, when I attempt to, they are not accessible.

I am getting an error as if they did not exist.

For example:

I am trying to access dbconnect.php, which is located in my root directory.
The domain is [url]www.mydomain.com[/url]

I made an admin folder and I need to access dbconnect.php, however, I DO NOT want to put the full URL in the event that I need to transfer scripts to other sites.

The work directory I am currently in is [url]www.mydomain.com/admin/includes[/url]

I need to access dbconnect.php, located in [url]www.mydomain.com/[/url]

I have tried the following and failed:

[code=php]<?
$root_path = ‘./’;
include($root_path . ‘dbconnect.php’);

?>[/code]

[code=php]<?php include(‘./dbconnect.php’);?>[/code]

Anyone know how to do this without me having to type

[code=php] <?php include(‘http://www.mydomain.com/dbconnect.php’);?>[/code]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@bokehMar 05.2007 — [code=php]$root_path = './';[/code][/QUOTE]That is a relative path based on the current directory.

If you server is configured correctly root would be:[code=php]$root_path = '/';[/code]
Copy linkTweet thisAlerts:
@gc40authorMar 05.2007 — I tried both './' and '/'

Both with no luck. Any other advice for accessing a .php file from my root directory?
Copy linkTweet thisAlerts:
@bokehMar 05.2007 — Run the following (from the directory where your script resides) and post the result[code=php]<table border="1" cellspacing="0" cellpadding="6">
<?php

foreach(array('DOCUMENT_ROOT','HTTP_HOST','PHP_SELF','REQUEST_URI','SCRIPT_FILENAME','SCRIPT_NAME') as $key)
{
if(isset($_SERVER[$key]))echo "<tr><td>$key</td><td>{$_SERVER[$key]}</td></tr>n";
}

?>
</table>[/code]
Copy linkTweet thisAlerts:
@gc40authorMar 05.2007 — DOCUMENT_ROOT /home/username/domain.com

HTTP_HOST domain.com

PHP_SELF /admin/index.php

REQUEST_URI /admin/

SCRIPT_FILENAME /home/username/domain.com/admin/index.php

SCRIPT_NAME /admin/index.php
Copy linkTweet thisAlerts:
@jasonahouleMar 05.2007 — There you have it.

Document root is right there. I would guess you are using third part hosting. This is the way most companies set up their servers. Set your $root_path = "/home/username/domain.com". That should solve your problem.
Copy linkTweet thisAlerts:
@bokehMar 05.2007 — if dbconnect.php really is in your root directory ([I]domain.com[/I]) you should be able to access the file like this: [code=php]include('/dbconnect.php')[/code]Don't forget the file name will be case-sensitive under a *NIX environment.
Copy linkTweet thisAlerts:
@jasonahouleMar 05.2007 — Correct, but the document root is not the root directory. Using include('/dbconnect.php') will put him at the root of the server. He needs to be at document root.

My suggestion is to either define

$root_path = "/home/username/domain.com"

or simply use

include('dbconnect.php')
Copy linkTweet thisAlerts:
@jasonahouleMar 05.2007 — Another alternative is to use

$_SERVER['DOCUMENT_ROOT']

rather than your self defined $root_path
Copy linkTweet thisAlerts:
@bokehMar 06.2007 — Correct, but the document root is not the root directory. Using include('/dbconnect.php') will put him at the root of the server. He needs to be at document root.

My suggestion is to either define

$root_path = "/home/username/domain.com"

or simply use

include('dbconnect.php')[/QUOTE]
[code=php]<?php

chdir('/');
echo getcwd();

?>[/code]
That does point to server root so you are correct. I think the confussion is caused because the difference in position between document root and http root. From a webpage "/" points at the same directory as document root (for obvious reasons).

The problem with $_SERVER['DOCUMENT_ROOT'] is it is often wrong if the server is running dynamic virtual hosts.
Copy linkTweet thisAlerts:
@MrCoderMar 06.2007 — If its a linux server you should also be able to do the following that will tell you the current directory that PHP is looking in.

[code=php]
<?php
system("pwd");
?>
[/code]
×

Success!

Help @gc40 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...