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
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 />";}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.