opendir() Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

opendir() Problem

Post by AliasBDI »

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]
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

I solved the problem by adding "$_SERVER['DOCUMENT_ROOT']" to the folder path. Thanks. :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Be aware that a file's extension has no baring on what kind of file it is.
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Post by AliasBDI »

Do you mean that my "if (preg_match('/\.'.$ext.'$/i', $file, $test))" is not working? I am trying to only allow extension set in my $exts. Am I doint it wrong?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Do you not want the files to be labeled what they really are?

getimagesize() and mime_content_type() may be of interest.
Post Reply