Forum Moderators: coopster
I've got a simple question I hope the pro's around here can answer. I have a website with multiple sub-directories and I use PHP 'includes' to assemble my template. I am wondering how to properly format links and files so they can be accessed anywhere from the root to 3 directories down.
Wrong way
<a href="../cars/index.php">
Correct way?
<a href="$DOCUMENT_ROOT/cars/index.php">
Wrong way
<img src="../images/logo.gif" alt="" width="372" height="53" border="0">
Correct way?
<a href="$DOCUMENT_ROOT/images/logo.gif" alt="" width="372" height="53" border="0">
Thanks!
KOB
You should format the links using the full path from the public root.
If you have an images folder that sits in the root(public) folder, then use /images/file.gif
Just remember to start your links at root(/) then use the true path from there.
<a href="/">this link will go the the home page no matter what directory you are currently in</a>
For example:
<?php include '/inc/header.php';?> called from /cars/ gives this error
Warning: main(/inc/header.php): failed to open stream: No such file or directory in /home/u5/html/cars/index.php on line 13
Warning: main(): Failed opening '/inc/header.php' for inclusion (include_path='.:') in /home/u5/html/cars/index.php on line 13
Do I have to resort to this?
<?php include '../inc/a-header.php';?>
Your original solutions is GOOD for includes.
include($_SERVER['DOCUMENT_ROOT'] . '/inc/a-header.php');
The doc_root gets the path from server root to public root(eg. /home/user/html), then you add the path from there as you would if you were doing a regular link.
.
..
[images]
[includes]
[articles]
index.php
If you are inside the index.php and want to include something inside the includes, simply do like this:
<?php
include("includes/the_file.php");
?>
if you are inside the articles folder and want to include something in the includes folder:
<?php
include("/includes/the_file.php");
?>