Page 1 of 1

Absolute path question

Posted: Fri Feb 26, 2010 6:17 pm
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

Re: Absolute path question

Posted: Fri Feb 26, 2010 6:58 pm
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" />

Re: Absolute path question

Posted: Fri Feb 26, 2010 8:43 pm
by JackD
Thanks... that appears to be the equivalent of doing '../standard.php' in this instance. Why doesn't '/standard.php' work?

Re: Absolute path question

Posted: Fri Feb 26, 2010 9:06 pm
by requinix
/ means root. In Windows lingo, you told it to look for "C:\standard.php".

Re: Absolute path question

Posted: Fri Feb 26, 2010 9:16 pm
by JackD
Thanks!