Skip to content
Advertisement

file_exists() not working codeigniter

I am working with codeigniter. I want to display images but if some image is not exist it should show image-not-found-medium.jpg which is dummy image..

below is my code

<?php
    $image_path_medium = site_url('assets/images-products/medium');
    $image_not_found_medium =  $image_path_medium . "/" . "image-not-found-medium.jpg";
    $image_name_with_path = $image_path_medium . "/" . $home_technology[$key]->product_sku . "-" . "a" . "-medium.jpg";

    if (file_exists($image_name_with_path)) {
        echo $image_name_with_path;
    } else {
        echo $image_not_found_medium;
    }
    ?>

but it always shows $image_not_found_medium i think there is problem with my if condition. Please help.

Advertisement

Answer

<?php
    $image_path_medium = site_url('assets/images-products/medium');
    $image_not_found_medium =  $image_path_medium . "/" . "image-not-found-medium.jpg";
    $image_name_with_path = $image_path_medium . "/" . $home_technology[$key]->product_sku . "-" . "a" . "-medium.jpg";//this is your image url
    $image_file_path=FCPATH.'assets/images-products/medium'. $home_technology[$key]->product_sku . "-" . "a" . "-medium.jpg";//this is your file path

   if (file_exists($image_file_path)) //file_exists of a url returns false.It should be real file path
   {
       echo $image_name_with_path;
   } 
   else 
   {
      echo $image_not_found_medium;
   }
?>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement