Jump to content

displaying a default image


unistake

Recommended Posts

Hi all,

 

I have this script below where I am trying to display a default image if an image can not be found.

 

For some reason though it is not working.

 

<?php 
foreach (glob('./aircraft/' . $rowX['reg'] . '[0-8].jpg') as $file)
{
   if (file_exists($file)) {
echo "<img src=\"" . $file . "\"  /><br />";
}
else { echo "><img src=\"aircraft/wrightflyer.jpg\" /><br />";}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/218387-displaying-a-default-image/
Share on other sites

I'm not very familiar with "glob" but after looking at it, it only finds files that EXISTS so you will never run into a file that doesn't exist.

 

try this ?:

$files = glob('./aircraft/' . $rowX['reg'] . '[0-8].jpg') or array();
foreach($files as $file){
  if (!empty($file)) {  // GLOB already determined the file exists, check if array is empty
echo "<img src=\"" . $file . "\"  /><br />";

}

else { echo "><img src=\"aircraft/wrightflyer.jpg\" /><br />";}
}

hmm.. ok .. maybe a for statement...

for($i = 0; $i <= 8; $i++){
  $filename = './aircraft/' . $rowX['reg'] . $i .'.jpg';
  if(file_exists($filename){
     echo "<img src=\"" . $filename . "\"  /><br />";
  } else {
    echo "><img src=\"aircraft/wrightflyer.jpg\" /><br />";
  }
}

 

This way you run thru all 0-8 occurences and can check each individual file without going thru the glob array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.