narjis Posted March 1, 2013 Share Posted March 1, 2013 i am trying to show images on my show a images of my products and checking the image files using file_exists function although my images are present in the correct path, images are not showing instead no image found is being displayed. Here in my code: foreach($products as $pros){ if(!file_exists(site_url()."theme/default/images/". $pros['image'])) $img = site_url()."theme/default/images/noimage.jpg"; else $img = site_url()."theme/default/images/". $pros['image']; ?> <li> <div class="prodImage"> <a href="<?=site_url()?>/marketplace/products/<?= $pros['id']?>"> <img src="<?=$img?>" alt='No image' /> </a> </div> <div class="ptitle"><?=$pros['name']?></div> <?php echo "<div class=\"description\">".$pros['description']."</div>"; $attribs = array('id'=>'css_id', 'class' =>'viewLink'); echo "<div class=\"view\">".anchor('marketplace/products/'.$pros['id'],'View',$attribs)."</div>"; ?> <div class="clear"></div> </li> <?php }?> </ul> Link to comment https://forums.phpfreaks.com/topic/275081-urgent-help-required/ Share on other sites More sharing options...
samitrimal Posted March 19, 2013 Share Posted March 19, 2013 You can do if(!file_exists($_SERVER['DOCUMENT_ROOT']."theme/default/images/". $pros['image']))$img = site_url()."theme/default/images/noimage.jpg";else $img = site_url()."theme/default/images/". $pros['image']; ?> Link to comment https://forums.phpfreaks.com/topic/275081-urgent-help-required/#findComment-1419516 Share on other sites More sharing options...
vikramjeet.singla Posted March 25, 2013 Share Posted March 25, 2013 file_exists works same for directories as well. So if you have image name empty in database so that path becomes from this site_url()."theme/default/images/". $pros['image'] to site_url()."theme/default/images/" which is a valid and available directory for file_exists function. So, always use a file_exists function with combination of is_file as below: if(!file_exists(site_url()."theme/default/images/". $pros['image']) && !is_file(site_url()."theme/default/images/". $pros['image'])){ // diplay no image } else { // display actual image } Link to comment https://forums.phpfreaks.com/topic/275081-urgent-help-required/#findComment-1420919 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.