Journal #6471

Posted 14 years ago2010-03-29 15:54:59 UTC
Need help with PHP!

I have a page that displays images from a directory passed through GET. I use this code to find out if the requested directory has images:

$eventcode = trim($_GET['event'])

(snip)

$content = glob(strtolower($eventcode) . '/*.{jpg, JPG, jpeg, JPEG, png, PNG}', GLOB_BRACE);


However, I just found out that the glob instruction above returns false when all images have uppercase extension. Am I overlooking something?

Any insight will be greatly appreciated.

2 Comments

Commented 14 years ago2010-03-29 16:13:37 UTC Comment #61902
how about something like this:

$dir = strtolower($eventcode) . '/*';
foreach (glob($dir) as $file) {
if (preg_match('/^.*\.(jpg|png)$/i', $file)) {
    $content[] = $file;
}
}

if that still doesn't work try another way.
Commented 14 years ago2010-03-29 20:55:14 UTC Comment #61903
Hm, that's a different approach. I'll give it a try. Thanks!

Edit:
After taking a look at the link you posted, I went for a different approach. The advantage is that I get the same kind of array out of this piece of code, and I don't need to alter the rest of the code below it.

[i]$filetypes = array('jpg','jpeg','png'); //Possible image extensions
$content = array(''); //1-empty-element array, so it's accepted as an array
if($handle = opendir($eventcode))
{
while( ($file = readdir($handle)) !== false)
{
	foreach($filetypes as $filetype)
	{
		if(strcmp(strtolower(substr($file, strlen($file) - strlen(strrchr($file, '.')) + 1 )), $filetype)==0)
		{
			array_push($content, $eventcode.'/'.$file);
		}
	}
}
closedir($handle);
array_shift($content); //Remove the empty first element
sort($content, SORT_STRING);
}else
{
$content = false;
}[/i]

Longer. Way longer. But appears to work well.

Edit: Pretend the TWHL engine displays the indentations too.

You must log in to post a comment. You can login or register a new account.