Re: [NEWBIE] Confused about usage of $_SERVER['DOCUMENT_ROOT']

From: Date: Sun, 20 Feb 2005 18:55:16 +0000
Subject: Re: [NEWBIE] Confused about usage of $_SERVER['DOCUMENT_ROOT']
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-209086@lists.php.net to get a copy of this message
Okay, for that last possibility, the one that you used the document root variable. You have to add your domain name before, the reason being is because it's relative to where the script is, if you add the domain name (http://www.whatever.org/document/root) then it switches from relative to absolute. Dave wrote:
Andre, Mirco, BDT, Thank you for your responses. Here is some test code <?php echo "<img src=\"inline/topimage.jpg\" >"; echo "<img src=\"/inline/topimage.jpg\" >"; echo "<img src=\"" . $_SERVER['DOCUMENT_ROOT'] . "/inline/topimage.jpg\" >"; ?>: Theoretically, they should all output the same image. However: <img src="inline/topimage.jpg" > ->IMAGE DISPLAYS <img src="/inline/topimage.jpg" > ->IMAGE DISPLAYS <img src="/home/sites/sitexxx/web/inline/topimage.jpg" > IMAGE DOES NOT DISPLAY Of course, the first instance only works if the script is in the correct place relative to the /inline directory. However, the second case fails if I use it in the following function (assuming I specify $dir as "/inline"). It returns a false result, as if it couldn't find the directory. function random_img($dir,$type='random') { global $errors,$seed; if (is_dir($dir)) { $fd = opendir($dir); $images = array();
     while (($part = @readdir($fd)) == true) {
         if ( eregi("(gif|jpg|png|jpeg)$",$part) ) {
             $images[] = $part;
         }
     }
// adding this in case you want to return the image array if ($type == 'all') return $images; if ($seed !== true) {
     mt_srand ((double) microtime() * 1000000);
     $seed = true;
}
         $key = mt_rand (0,sizeof($images)-1);
return $dir . $images[$key]; } else {
     $errors[] = $dir.' is not a directory';
     return false;
} }


« previous php.general (#209086) next »