Jump to content

Setting Root


Drongo_III
Go to solution Solved by Jacques1,

Recommended Posts

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

Link to comment
Share on other sites

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?  

Link to comment
Share on other sites

  • Solution

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';
Link to comment
Share on other sites

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.

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.