Forum Moderators: coopster

Message Too Old, No Replies

Show image with unknown name

         

zenon84

4:39 pm on Mar 30, 2009 (gmt 0)

10+ Year Member



Hello.

I have a folder with many images with different names, for example 123_456_789_***_***.bmp
The "123", "456", "789" are values that i get from a database and the "***" can be different characters, that are different for each image.
I want to show these images but i can't find a way to 'search' in the name of the image, in order to find "***" characters and use it.

For example:
echo "<img src='images/".$id1 ."_" .$id2 . "_" . $id3 . "_" .*** . "_" . *** . ".bmp' >";

Is there a way to solve this problem?

Thanks a lot!

d40sithui

6:33 pm on Mar 30, 2009 (gmt 0)

10+ Year Member



I can't think of any simple solution. I also wonder why you would design it this way. Wouldn't it be simpler to store the full file name in the db so you can get it later on?
Anyway, one way is to go through the whole directory, scan and save all the image files there to an array, then you can have the data readily accessible to work with. Hope this helps.

function getImages($dir){
$fulldir = $_SERVER['DOCUMENT_ROOT']."/$dir";
$images = array();

//if directory is valid
if(is_dir($fulldir)){
$dh = opendir($fulldir);
if($dh){
while(($file = readdir($dh)) != false){

if(preg_match( "/[a-zA-Z0-9\_\.\-\s]+\.jpg¦bmp¦gif¦png$/", $file)){
//echo "filename: $file : filetype: " . filetype("$file") . "<br>";
$images[] = array(
"file" => "/$fulldir/$file",
"name" => substr($file, 0, -4),
"type" => substr($file, -3));
}

}
}
closedir($dh);
return $images;
}

}

$myImages = getImages("images/icons");

zenon84

6:43 pm on Mar 30, 2009 (gmt 0)

10+ Year Member



Hello!
Thank you very much!
I want this way of design, because someone can take a picture and name it for example 1_2_hisName.bmp .

I want to save the picture with that name and display it according its id "1_2".

Someone from another forum told me to use glob() and i'm trying this way!

But thank you very much for your interest!