1

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.

  • Maybe `site_url()` returns something like `http://my-site.com/assets/images-products/medium`? If you want to use `file_exists()`, you have to provide a path to the filesystem where your code is running. Try replacing that line to be like this: `$image_path_medium = $_SERVER['DOCUMENT_ROOT'].'/assets/images-products/medium';`. – Ismael Miguel Jan 14 '15 at 09:36
  • Looking at https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html confirms what I've stated above. – Ismael Miguel Jan 14 '15 at 09:42
  • done changes as per your suggestion.. but now also its avoiding `if condition` –  Jan 14 '15 at 09:47
  • Did you tried with a file that you are 100% that doesn't exist? – Ismael Miguel Jan 14 '15 at 09:48
  • Can you, in a different file, do something like this: `readfile('[path that was echoed on the other code]');`? – Ismael Miguel Jan 14 '15 at 09:51
  • i have tried `$_SERVER['DOCUMENT_ROOT']` & it showing `/home/servername/public_html/my-site.com/assets/images-products/medium/imagename.jpg` –  Jan 14 '15 at 09:55
  • Now, you create a new php file with `readfile('/home/servername/public_html/my-site.com/assets/images-products/medium/imagenam‌​e.jpg');` in it. Tell me the result after – Ismael Miguel Jan 14 '15 at 09:56
  • Now it only outputs `image-not-found-medium.jpg` –  Jan 14 '15 at 10:07
  • Do you know if you have any opcache enabled? You should check http://stackoverflow.com/questions/17224798/how-to-use-php-opcache to know how to use it. Reading there, you should try to output run `var_dump(opcache_get_status());` on a new php file – Ismael Miguel Jan 14 '15 at 10:13

4 Answers4

4
<?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;
   }
?>
  • it showing `/home/servername/public_html/my-site.com/assets/images-products/medium/imagenam‌​e.jpg` –  Jan 14 '15 at 10:04
  • thanks, but it also didnt worked it always avoids `if condition` even if the image is available.. –  Jan 14 '15 at 10:20
  • what does `$image_file_path` and `$image_name_with_path` prints –  Jan 14 '15 at 10:26
  • you said it was showing `/home/servername/public_html/my-site.com/assets/images-products/medium/imagenam‌​‌​e.jpg` now you saying file exists not working. echo `$image_file_path` and `$image_name_with_path` before if condition. and what you got.just write the full output.do not write whats going on –  Jan 14 '15 at 11:02
  • Thanks it worked, but `/` was missing in line `$image_file_path=FCPATH.'assets/images-products/medium'. $home_technology[$key]->product_sku . "-" . "a" . "-medium.jpg";` before `$home_technology[$key]->product_sku` –  Jan 17 '15 at 06:53
0

You are using absolute path for file existence which is wrong. You have to use real path because the file_exists() function checks whether or not a file or directory exists on the current server.

If your assets folder is placed in root then just use getcwd() - Gets the current working directory as

$image_path_medium = getcwd().'assets/images-products/medium';

Otherwise give the proper path to the assets folder like

$image_path_medium = getcwd().'application/views/assets/images-products/medium';
Robin Garg
  • 203
  • 2
  • 13
0

Instead of file_exists() prefer is_file() when checking files, as file_exists() returns true on directories. In addition, you might want to see if getimagesize() returns FALSE to make sure you have an image.

Kavita
  • 77
  • 7
0

Use like this.

$g = base_url().'upload/image.jpg';
if(file_exists($g) !== null){//your code..}

This is works for me in CI.

4b0
  • 21,981
  • 30
  • 95
  • 142
  • 1
    Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – Ismael Padilla Jan 23 '20 at 11:40