note 25712 added to function.include

From: Date: Wed, 02 Oct 2002 22:48:58 +0000
Subject: note 25712 added to function.include
Groups: php.notes 
Request: Send a blank email to php-notes+get-37574@lists.php.net to get a copy of this message
I develop on a local Apache server, but my host is IIS. Imagine my amazement when I found that IIS doesn't set $DOCUMENT_ROOT! I wanted to use absolute includes (none of this "../phpfile.php" nonsense, which can break if I move the page). After looking around, I found out that IIS does set PATH_TRANSLATED and PATH_INFO. Examples: PATH_TRANSLATED = c:\\root\\subdirectory\\phpfile.php PATH_INFO = /subdirectory/phpfile.php Here's what I came up with: // if $DOCUMENT_ROOT does not exist, build one from scratch out of PATH_TRANSLATED and PATH_INFO. $include_path = (!$_SERVER['DOCUMENT_ROOT']) ? str_replace($_SERVER["PATH_INFO"], "", str_replace("\\\\", "/", $_SERVER["PATH_TRANSLATED"])) : $_SERVER['DOCUMENT_ROOT']; I str_replace the \\ in PATH_TRANSLATED to /, then cut PATH_INFO off of it. This returns what would normally be in $DOCUMENT_ROOT. Put THIS in the beginning of every file, then write your includes as include($include_path . "/phpfile.php"); Then you'll never have to rewrite it again, even if you shuffle your directories. -- http://www.php.net/manual/en/function.include.php http://master.php.net/manage/user-notes.php?action=edit+25712 http://master.php.net/manage/user-notes.php?action=delete+25712 http://master.php.net/manage/user-notes.php?action=reject+25712

« previous php.notes (#37574) next »