Drongo_III Posted October 23, 2015 Share Posted October 23, 2015 Hello Hope someone can help and I'm sure this is a simple one. I'm using a local copy of XAMPP at the moment and have a basic issue but I've been searching around and struggling to find the answer. Usually when I work on a site, lets say it's domain is example.com, and I wish to include a file from a location like example.com/includes/, I would simply use a php's include as follows: <?php include('/includes/some-file.php'); ?> In this instance I'd expect the '/' to refer to the base domain - i.e. example.com - so that irrespective of where I include the file from it always has a sound reference. However, on my local xampp i'm having issues. So lets say I have a file I want to include located in in: /htdocts/ng/includes/some-file.php And I try to include it from a sub directory /htdocs/ng/some-dir/ as per the code posted above I get an error: "No such file or directory in C:\xampp\htdocs\ng\some-dir\some-file.php" It's as if the include path is always trying to include from the current directory. So is there a setting in apache I need to change to ensure that the base domain is always referenced? Hope that makes sense, Drongo Quote Link to comment https://forums.phpfreaks.com/topic/298797-setting-root/ Share on other sites More sharing options...
benanamen Posted October 23, 2015 Share Posted October 23, 2015 (edited) If I understand your example it would be include('../includes/some-file.php'); for a file located here: /htdocs/ng/some-dir/ to include a file located here: /htdocts/ng/includes/some-file.php Edited October 23, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298797-setting-root/#findComment-1524188 Share on other sites More sharing options...
Drongo_III Posted October 23, 2015 Author Share Posted October 23, 2015 Yeah I could use a relative path but on other servers I've worked on they've set them up so you just use /includes/file-name.php and it doesnt matter where you then make the call from it still works. I guess I was just trying to work out how to configure xampp so it always looks at /htdocs/ as it's root when including files? Quote Link to comment https://forums.phpfreaks.com/topic/298797-setting-root/#findComment-1524189 Share on other sites More sharing options...
Solution Jacques1 Posted October 23, 2015 Solution Share Posted October 23, 2015 Using absolute paths and then expecting them to somehow be turned into relative paths is a really, really bad idea. The path /includes/file-name.php is absolute and literally means: “Right below the root filesystem, there's an includes directory with the script file-name.php”. This obviously makes no sense. Maybe your Windows PC interprets the path differently, but all Unix-based servers will see an absolute path. If you want a path to be relative to some base directory, then you actually need a relative path. Use the include_path directive to set the base directory: // assuming C:/htdocs/ng is in your include_path include 'includes/some-file.php'; Quote Link to comment https://forums.phpfreaks.com/topic/298797-setting-root/#findComment-1524190 Share on other sites More sharing options...
Drongo_III Posted October 23, 2015 Author Share Posted October 23, 2015 Thanks for the advice! Quote Link to comment https://forums.phpfreaks.com/topic/298797-setting-root/#findComment-1524193 Share on other sites More sharing options...
scootstah Posted October 23, 2015 Share Posted October 23, 2015 In this instance I'd expect the '/' to refer to the base domain - i.e. example.com - so that irrespective of where I include the file from it always has a sound reference. You're confusing the way HTML works with how filesystems work. The filesystem has no idea what "example.com" is. Filesystem directories are not based on domain names. On UNIX-based systems the filesystem is a tree structure, and begins with a "/". The "/" is referred to as "root". By using a "/" at the beginning of a path name, you are starting from the root of the filesystem. That is an absolute path. If you want to start from any other directory you would use a relative path, by omitting the "/" from the beginning. Since you're on Windows, a "/" means the root of the partition that the script was executed from. So if your PHP file is located in C:\, then "/" would refer to C:\. If your script is located in F:\, then "/" would refer to F:\. Yeah I could use a relative path but on other servers I've worked on they've set them up so you just use /includes/file-name.php and it doesnt matter where you then make the call from it still works. You must be mistaken, because that's simply not how it works. Quote Link to comment https://forums.phpfreaks.com/topic/298797-setting-root/#findComment-1524194 Share on other sites More sharing options...
mac_gyver Posted October 23, 2015 Share Posted October 23, 2015 if your server is set up correctly, and if it's not, you can fix to be so, $_SERVER['DOCUMENT_ROOT'] refers to the document root folder and can be used for building an absolute file system path for include/require statements. Quote Link to comment https://forums.phpfreaks.com/topic/298797-setting-root/#findComment-1524195 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.