You are here: Re: Relative Paths and include files « PHP Programming Language « IT news, forums, messages
Re: Relative Paths and include files

Posted by Geoff Muldoon on 07/18/05 05:01

mark@something.com says...

> Can anybody share some tips on what they do to overcome what must be a
> very common problem? Is absolute paths the answer so instead of calling
> ../images/image.gif from the include file, I would call /images/image1.gif?

My usual technique is to create a "config" file and populate it with a
number of defined CONSTANTS (note: NOT variables) and then include it in
each of the PHP files in my web application.

Simplified example:

<?php // config file
define('WEB_ROOT', 'http://http_doc_root/');
define('FILE_ROOT', '/absolute_php_root/');
define('IMAGE_WEB_PATH', WEB_ROOT.'images/');
define('IMAGE_FILE_PATH', FILE_ROOT.'images/');
define('INCLUDE_FILE_PATH', FILE_ROOT.'includes/');
define('HEADER_FILE', INCLUDE_FILE_PATH.'header.php');
define('FOOTER_FILE', INCLUDE_FILE_PATH.'footer.php');
?>

<?php // sample app file
//
// first line below is only entry for this file where you might use a full
// path but I usually use a relative one as I will know where this app
// file is in the file system eg. maybe include '../includes/config.php'
// instead
//
include 'includes/config.php';
include HEADER_FILE;
echo 'This is a picture: <br />';
echo '<img src="'.IMAGE_WEB_PATH.'picture.jpg" alt="pic">
include FOOTER_FILE;
?>

Also excellent for portability within or across hosts, simply adjust in
the config file only.

Geoff M

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация