The correct absolute path for PHP include from another directory?

Status
Not open for further replies.

sperko

Member
Messages
258
Reaction score
4
Points
18
Hey all

I am including PHP files from other directories on my site, I am using /home/sperko/public_html/ at the beginning of my include, but when I download my files to develop my site on my localhost, the file path /home/sperko/public_html/ does not exist, since it only applies to my x10host account and throws up PHP errors and I can't seem to get the paths to work, are there any alternative include paths that will work both on a live server (X10host) and my localhost?

LONG VERSION OF THIS PROBLEM W/ MORE DETAILS:

I will try to keep this as simple as possible, I have a directory with a file for example mywebsite.com/my_folder/index.php, which includes a PHP file from another directory like mywebsite.com/other/my_php_file.php

The include on index.php looks like this:
Code:
('/home/sperko/public_html/other/my_php_file.php')

Is it required that I use the following at the beginning of the path? Because it doesn't seem to work without it:
Code:
/home/sperko/public_html/

This is where it gets confusing for me, but it kinda makes sense but I need some help

If the PHP file I want to include is in a sub-directory within the main directory, for example mywebsite.com/my_folder/index.php includes the file from mywebsite.com/my_folder/other/my_php_file.php then this include I use on index.php works without any problems:
Code:
('other/my_php_file.php')

But I want to keep the folder-structure I currently have which brings me to my problem...

The main problem is I tried to develop my site offline in XAMP but when I download my database and site files to view them locally, the PHP includes do not work and I get the following error, all the includes from the sub-directories of main directories work OK, but the cross-directory includes do not since they use
Code:
/home/sperko/public_html/

which my local site doesn't understand, probably because of the folder-structure of my x10host account

I get these types of errors on my local machine:

Code:
Warning: require_once(/home/sperko/public_html/other/my_php_file.php): failed to open stream:
No such file or directory in C:\xampp\htdocs\localwebsite\my_folder\index.php on line 29

If you've made it this far, great! I really hope you can advise me on this :)
 
Last edited:

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
Code:
$_SERVER["DOCUMENT_ROOT"] . "/other/my_php_file.php"

will give you the string "/home/sperko/public_html/other/my_php_file.php" on your x10 account.

Not 100% sure if it will work on XAMPP
 

sperko

Member
Messages
258
Reaction score
4
Points
18
Thanks I tried that on some files and the errors are no longer there, but I get a blank page now and the error log is clear...

I will look further into my code to see if I missed anything
 

sperko

Member
Messages
258
Reaction score
4
Points
18
Still can't get it to work, I also added a SPOILER tag so you can toggle the long version if you want to read further into my issue
 
Last edited:

sperko

Member
Messages
258
Reaction score
4
Points
18
Does anyone else know if I can use an alternative path to my PHP includes or would I have to use /home/sperko/public_html/
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
XAMPP should be returning "C:/xampp/htdocs" for $_SERVER['DOCUMENT_ROOT']. If necessary, you can include a further config file in the server root that redefines the path if your site is in a folder on your local install and in the public_html root folder on the server, or use an if statement that looks for "C:" or "home" in the returned path.
 

sperko

Member
Messages
258
Reaction score
4
Points
18
I have the root folder set in httpd-vhosts.conf and when I access my sites front-page directory in XAMP I get this error
Code:
Warning: require_once(/home/sperko/public_html/my_random_folder/included_file.php): failed to open stream:
No such file or directory in C:\xampp\htdocs\my_xamp_site\front-end\index.php on line 29

I don't actually think this is a XAMP issue as I think it's configured correctly and the problem is with the specific include path, I think I need to adjust the include path on my x10host files so they don't use the home directory /home/sperko/public_html/ - so when transferred over to XAMP the same includes will work

Thanks for replying!
 

sperko

Member
Messages
258
Reaction score
4
Points
18
The contents of httpd-vhosts.conf if needed: (host file has also been changed)
Code:
    <VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs"
    ServerName localhost
    </VirtualHost>
   
    <VirtualHost *:80>
    DocumentRoot "C:/xampp/htdocs/my_xamp_site"
    ServerName test.dev
    ServerAlias www.test.dev
    <Directory "C:/xampp/htdocs/my_xamp_site">
    Order allow,deny
    Allow from all
    </Directory>
    </VirtualHost>
 

gamersc

Member
Messages
43
Reaction score
1
Points
8
usually means that the 2nd file your trying to include isn't set up to the right path. plus also, xampp isn't to good to use. i use wampp as its more user friendly.

if you wouldn't mind posting or sharing the files we may be able to help. even if you copied what the parts that includes the files onto here.
 

sperko

Member
Messages
258
Reaction score
4
Points
18
all the details are included in the spoiler tag on the original post, it's a bit of a long read though
 

sperko

Member
Messages
258
Reaction score
4
Points
18
An update from my post here

I think I'm nearly there... I changed my include back to the previously suggested $_SERVER['DOCUMENT_ROOT'] (which I already tried, but I think it's correct)

This works on my x10host account, but using the same path/include code gives me a blank page on XAMP and no error log entries:
Code:
require_once($_SERVER['DOCUMENT_ROOT'].'/include_from_here/my_file.php');
Which is great, no errors so it's TRYING to work, maybe I haven't configured something as the blank page suggests something is working... Still trying to figure it out!
 
Status
Not open for further replies.
Top