0
votes

I'm working on making my web app more portable but I'm having trouble with making links in a sub-directory root-relative.

/website - Let's say that's my directory for the website.
/inc - That's my includes directory for header, footer, and eventually my config file.

Everything inside the website directory works fine.

I'm trying to include my header/footer files in a receipt directory with..

include("/website/inc/header.php");

but I get..

Warning: include(/website/inc/header.php): failed to open stream: No such file or directory in F:\xampp\htdocs\website\receipt\index.php on line 8

Warning: include(): Failed opening '/website/inc/header.php' for inclusion (include_path='.;F:\xampp\php\PEAR') in F:\xampp\htdocs\website\receipt\index.php on line 8

in my browser. I've tried removing the / and even adding localhost and htdocs (which was just a desperate attempt at getting it to work). What am I doing wrong and how can I correct it?

1

1 Answers

0
votes

That's because a file path starting with / is treated as a file path starting from the root of file system. You have F:\ as a root that's why such a directory doesn't exist.

In your case you have to do something like ../../website/inc/header.php but that depends on the location of the folder where your script resists.

PHP (but not HTML!) works with whole file system, while HTML works only with the site's home directory.

By the way, such file paths are not allowed on Windows, they're only available on UNIX-like systems. If such a construction with /'s works on your server, that's strange as Windows has \ as path separator. Your code'll look like ..\..\website\inc\header.php. Note that you may need to escape the backslashes by adding another backslash like this \\