Hi,
Try this,
$dh = opendir("images/book_cover") or die ("Directory Open failed!");
while (($file = readdir($dh)) !== false)
{
if($file <> '.' && $file <> '..')
{
$book_cover_str = explode(".",$file);
$title="basic cooking";
$title_str = str_replace(' ','_',trim($title));
if ($book_cover_str[0]==$title_str)
{
$fileName = 'images/book_cover/'.trim($file);
echo $fileName;
}
}
}
closedir($dh);
But what i dont understand is your requirement. Your just trying to find whether specific file exists in the image folder and if exists display it right.
If so then why do you read through all the folder. assume if you have 1000 titles are you trying to repeat this code for 1000 times?
so why dont you do this as i told you earlier.
$title = 'Basic Cooking';
$fileName = 'images/book_cover/'.str_replace(' ','_',trim($title)).'.jpg';
if(file_exists($fileName))
{
print "<img src='".$fileName."' />";
}
in this case its more dynamic where you check the specific file (not whole directory though a loop) and if that file exists display or do what ever you like else simply ignore.
Sorry if i misunderstood your requirement and hope this helps.
Regards,
niroshan