Punk Rock Geek Posted October 28, 2009 Share Posted October 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/ Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-945975 Share on other sites More sharing options...
Punk Rock Geek Posted October 28, 2009 Author Share Posted October 28, 2009 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'] Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-945980 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-945992 Share on other sites More sharing options...
Punk Rock Geek Posted October 28, 2009 Author Share Posted October 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-945995 Share on other sites More sharing options...
taquitosensei Posted October 28, 2009 Share Posted October 28, 2009 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" Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-945997 Share on other sites More sharing options...
Punk Rock Geek Posted October 28, 2009 Author Share Posted October 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-945999 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946010 Share on other sites More sharing options...
newbtophp Posted October 28, 2009 Share Posted October 28, 2009 perhaps try include $_SERVER['DOCUMENT_ROOT'].'/file.php'; or require $_SERVER['DOCUMENT_ROOT'].'/file.php'; Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946015 Share on other sites More sharing options...
Punk Rock Geek Posted October 28, 2009 Author Share Posted October 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946018 Share on other sites More sharing options...
Punk Rock Geek Posted October 28, 2009 Author Share Posted October 28, 2009 perhaps try include $_SERVER['DOCUMENT_ROOT'].'/file.php'; or require $_SERVER['DOCUMENT_ROOT'].'/file.php'; Didn't work. Thanks though. Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946019 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 try "http://".$_SERVER['HTTP_HOST']."/plugins/content/jumi/originals/language/file.php" Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946023 Share on other sites More sharing options...
Punk Rock Geek Posted October 28, 2009 Author Share Posted October 28, 2009 try "http://".$_SERVER['HTTP_HOST']."/plugins/content/jumi/originals/language/file.php" Nope, nothing. Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946027 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 Can you be more descriptive? Your one-line comments are not helping at all. What do you mean "nothing"? the variable's empty? nothing gets included? it is not working like you want it too? Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946058 Share on other sites More sharing options...
Punk Rock Geek Posted October 28, 2009 Author Share Posted October 28, 2009 Can you be more descriptive? Your one-line comments are not helping at all. What do you mean "nothing"? the variable's empty? nothing gets included? it is not working like you want it too? The variable isn't empty. It just isn't including anything. I don't know what else to add. Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946074 Share on other sites More sharing options...
seanlim Posted October 28, 2009 Share Posted October 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946078 Share on other sites More sharing options...
PFMaBiSmAd Posted October 28, 2009 Share Posted October 28, 2009 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.) Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946310 Share on other sites More sharing options...
knsito Posted October 28, 2009 Share Posted October 28, 2009 /var/www/html//plugins/content/jumi/originals/language/file.php" Hi, see the double //? include ($_SERVER['DOCUMENT_ROOT'] . 'directory/file.php'); Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946369 Share on other sites More sharing options...
PFMaBiSmAd Posted October 28, 2009 Share Posted October 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946393 Share on other sites More sharing options...
knsito Posted October 28, 2009 Share Posted October 28, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946489 Share on other sites More sharing options...
Punk Rock Geek Posted October 28, 2009 Author Share Posted October 28, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179295-include-absolute-path-of-a-file/#findComment-946671 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.