Home > Back-end >  how do i display image with a specific file name using part of the file name
how do i display image with a specific file name using part of the file name

Time:07-24

I am working on a project where a lot of images are uploaded. Image name is saved as follows; $target_file = $target_dir .$Property_number . basename($_FILES["sittingroom"]["name"]);

i want to display images that have thesame $Property_number. So i used fnmatch() search for '62c8706c06bf2' but in return i get images with 62a456ggfrshg, 62bu737778384 and so on. I think this happens because the first two string matches. but i want is only when the whole search string is contained in the image name.

i used glob() but i think that is worst coz it didnt match anything. maybe i did it wrong. I need help please. I have searched but couldnt get anything that will help.

<?php

$detg = '62c8706c06bf2';
$imagesDirectory = "singlerooms/";
if(is_dir($imagesDirectory))
{
    $opendirectory = opendir($imagesDirectory);
    while (($image = readdir($opendirectory)) !== false)
    {
        if(($image == '.') || ($image == '..'))
        {
            continue;
        }
        
        $imgFileType = pathinfo($image,PATHINFO_EXTENSION);
        
        if(($imgFileType == 'jpg') || ($imgFileType == 'png')){
            fnmatch($image, $detg, $imagesDirectory = 0);{
            echo ("<img src='singlerooms/".$image."' width='150' > ");
      }
   }
 }
   closedir($opendirectory);
};

?>

CodePudding user response:

Keep it simple, try this:

if(($imgFileType=='jpg' || $imgFileType=='png') && strpos($image, $detg)!==false )
    echo "<img src='singlerooms/".$image."' width='150' > ";
  •  Tags:  
  • php
  • Related