You are here: Re: [PHP] [NEWBIE] Confused about usage of $_SERVER['DOCUMENT_ROOT'] « PHP « IT news, forums, messages
Re: [PHP] [NEWBIE] Confused about usage of $_SERVER['DOCUMENT_ROOT']

Posted by Dave on 02/20/05 20:22

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;
}
}

--
Dave Gutteridge
dave@tokyocomedy.com/

 

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

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