Jump to content

Include ("Absolute Path") of a file?


Punk Rock Geek

Recommended Posts

I'm not sure what I'm doing wrong.  I am writing my php code in the same directory of the file that I wish to include is in.  So, here's what I have written:

 

include ('file.php');

 

This works without any problems.  However, I wish to link to this file using an absolute path, rather than a relative one.  According to most online tutorials, I would use the following code:

 

include ($_SERVER['DOCUMENT_ROOT'] . '/directory/file.php');

 

However, this does not work.  Does anyone know what the problem might be?

Link to comment
Share on other sites

what is the error you are getting?

 

try

var_dump($_SERVER['DOCUMENT_ROOT'] . '/directory/file.php')

 

you might want to try the php realpath() function

 

When I use var_dump, I get this:

 

string(85) "/var/www/html//plugins/content/jumi/originals/language/file.php"

 

The bolded part is what is returned from $_SERVER['DOCUMENT_ROOT']

Link to comment
Share on other sites

Are you trying to get something like "http://www.yoursite.com/directory/file.php"?

 

If so, try using $_SERVER['SERVER_NAME'] instead.

 

However, be mindful that if you use an absolute path like this through http, only the OUTPUT of the file will be included. Functions and variables you declare in the PHP file cannot be accessed.

Link to comment
Share on other sites

Are you trying to get something like "http://www.yoursite.com/directory/file.php"?

 

If so, try using $_SERVER['SERVER_NAME'] instead.

 

However, be mindful that if you use an absolute path like this through http, only the OUTPUT of the file will be included. Functions and variables you declare in the PHP file cannot be accessed.

Hmm, nope.  I'll need access to the functions and variables still.

Link to comment
Share on other sites

Is there supposed to be another folder before plugins? I see a double slash looks like you're missing a folder.

string(85) "/var/www/html//plugins/content/jumi/originals/language/file.php"

I changed

include ($_SERVER['DOCUMENT_ROOT'] . '/directory/file.php');

to

include ($_SERVER['DOCUMENT_ROOT'] . 'directory/file.php');

And it got rid of the double slash.  Still doesn't work though.

Link to comment
Share on other sites

It doesn't work 'cos the file doesn't exist?

 

Just access the directory /var/www/html/ and check that the rest of the path (plugins/content/jumi/originals/language/file.php) exists. If it doesn't you are using the wrong $_SERVER variable.

 

Where this script that you are running relative to your www root directory?

Link to comment
Share on other sites

It doesn't work 'cos the file doesn't exist?

 

Just access the directory /var/www/html/ and check that the rest of the path (plugins/content/jumi/originals/language/file.php) exists. If it doesn't you are using the wrong $_SERVER variable.

 

Where this script that you are running relative to your www root directory?

The URL of the file I'm trying to include is:

 

http://www.mydomain.com/plugins/content/jumi/originals/language/file.php

Link to comment
Share on other sites

How do you know it isn't including the file? Does your file output some code?

 

Like I mentioned previously, only the OUTPUT of the file will be included. Functions and variables you declare in the PHP file cannot be accessed. If you want to access them, you will have to reference the file using a relative reference.

Link to comment
Share on other sites

Here is the $64,000 question. Is /var/www/html/ correct for your account on the server -

 

When I use var_dump, I get this:

 

string(85) "/var/www/html//plugins/content/jumi/originals/language/file.php"

 

The bolded part is what is returned from $_SERVER['DOCUMENT_ROOT']

 

A lot of web hosts don't set up the vhosts correctly so that $_SERVER['DOCUMENT_ROOT'] is populated with the value for your account. Given that your value ends with a / and nothing related to your domain/account, you should talk to your web host about this.

 

If they won't or don't know how to fix the setting, you can also set it to the correct value at the start of your script (once your script is running, it is just a variable and can be assigned a value like any other variable.)

Link to comment
Share on other sites

He already tried that.

 

$_SERVER['DOCUMENT_ROOT'] does not normally include the trailing slash and on shared web hosting it does include your account-name/domain-name as part of the path.

 

oh right. didnt see that.

 

The actual error message might be helpful

 

Link to comment
Share on other sites

With the code:

 

include ($_SERVER['DOCUMENT_ROOT'] . 'plugins/content/jumi/originals/language/file.php');

 

I am getting no errors.  The only error that shows up is this:

 

Notice: Undefined variable: language in /home/sitename/public_html/plugins/content/jumi/originals/file2.php on line 26

 

And that's only because the language variables within file.php are not being included.  I used:

 

error_reporting(E_ALL);
ini_set('display_errors', '1');

 

To report the errors.

 

 

Link to comment
Share on other sites

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.