Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Why does ../ work but not / for relative file paths?

Johna

Frontend Developer
Staff Team
Guardian
In my website, I have a header that is the same for every page, so rather than typing the code for the header in each page, I've created a file called header.php and then include it in each page using the include statement. When I type in this, it works:
PHP:
<?php include "../_data/header.php"; ?>

But when I use / instead of ../ it doesn't work, idk why?
PHP:
<?php include "/_data/header.php"; ?>
 
Last edited:
../ is a relative path that says the file is in the folder that is one level higher than the current directory.
./ is also a relative path that says the file is in the same folder as the current file.
/ is always the root of the current file.
 
This is how my files are arranged:
header.php
index.php

And this is inside index.php:
PHP:
<?php include "/_data/header.php"; ?>
 
Last edited:
Exactly like I said - using the / I get the error:
No such file or directory in C:\wamp64\www\VscPhp\home\index.php on line 2
(side note: I thought it would be C:\\. The root.)

But ../ goes up 2 folders - The first is home the second is _data


FYI I am using VSCode and Wampserver to do this on my comfuser.
 
But if both _data and home folders are in the root folder, shouldn't / work?

Because something like this works:
HTML:
<a href="/">root</a>
 
In that same index.php I have a linked stylesheet and it works perfectly.
So why does this work:
HTML:
<link rel="stylesheet" href="/_data/global-style.css">
but not this:
PHP:
<?php include "/_data/header.php"; ?>

It is something with PHP or the include statement?
 
This already been answered, but in linux/unix operating systems,
Code:
/
means root, which is the lowest and most important directory on the whole system (in this case, website): the largest container,
Code:
.
is the working or current directory, whatever position your shell is operating in...while
Code:
..
is the parent directory, the directory below/above the working directory.

Pretty much every server is linux/unix these days.
 
This already been answered, but in linux/unix operating systems,
Code:
/
means root, which is the lowest and most important directory on the whole system (in this case, website): the largest container,
Code:
.
is the working or current directory, whatever position your shell is operating in...while
Code:
..
is the parent directory, the directory below/above the working directory.

Pretty much every server is linux/unix these days.
We are not talking about the difference between how OSes handle `/`, we are talking about the difference in how HTML and PHP handle the `/` character. HTML handles it as go to the root of the directory being served. PHP handles it as go to the root of the drive.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom