Absolute path question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Absolute path question

Post by JackD »

On a linux system, I have a root directory with the following:

images (directory)
coolstf (directory)
standard.php

standard.php has the standard info for all pages, include image references for the header, sidebar, menu buttons, etc. Image references are simple, ie, 'images/myimage.jpg. Everything works fine for pages in the root directory.

In the directory coolstf, I have the file word.php in which requires standard.php. If, in word.php, I use

Code: Select all

<?php require('/standard.php'); ?>
, standard.php cannot be found. If I change the reference to

Code: Select all

<?php require('./standard.php'); ?>
, standard.php is found, but none of the image references are valid.

If I change an image reference in standard.php to be /image/myimage.jpg, it does not help. What am I doing wrong? How do I need to reference standard.php and the images within it?

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Absolute path question

Post by requinix »

When referencing PHP files, the easiest option is to use the DOCUMENT_ROOT. That's where the root of your website is located on the hard drive.

Code: Select all

include $_SERVER["DOCUMENT_ROOT"] . "/path/to/file.php";
When referencing files in HTML, use a leading / to indicate the root of the website.

Code: Select all

<img src="/path/to/image.jpg" />
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Absolute path question

Post by JackD »

Thanks... that appears to be the equivalent of doing '../standard.php' in this instance. Why doesn't '/standard.php' work?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Absolute path question

Post by requinix »

/ means root. In Windows lingo, you told it to look for "C:\standard.php".
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Absolute path question

Post by JackD »

Thanks!
Post Reply