opendir() Problem
Posted: Fri May 25, 2007 8:47 am
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I have a function (see below) that randomly selects images in a folder to print them to the browser. I have only been able to get it to work if I use relative paths. However, I am using search friendly URLs which are confusing the PHP of where the page is being parsed. For example, here is the path "./graphics/files". When this function is called by a page that is physically in the site root, it works because it looks in the "domain.com/graphics/files" folder. However, when this function is called by a page that is physically in the site root but the URL says that it is not, then it looks somewhere else depending on the URL.
Here is the example in other words:
The page 'domain.com/index.php' calls the function saying look here "./graphics/files/" and it works when the URL reads, "http://www.domain.com/index.php". However, the same page 'domain.com/index.php' calls the same function saying look here "./graphics/files/" and it does not work when the URL reads "http://www.domain.com/folder/folder2/index.php".
Any ideas?
Here is the code:Code: Select all
function tmp_imgHeader($folder,$txt){
$exts = 'jpg jpeg png gif';
$files = array();
$i = -1;
$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) {
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // case insensitive
$files[] = $file;
++$i;
}
}
}
closedir($handle);
mt_srand((double)microtime()*1000000);
$rand = mt_rand(0, $i);
echo $folder;
echo "<img src=\"".$folder.$files[$rand]."\" />";
}feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]