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.

9
  • 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';. Commented Jan 14, 2015 at 9:36
  • Looking at ellislab.com/codeigniter/user-guide/helpers/url_helper.html confirms what I've stated above. Commented Jan 14, 2015 at 9:42
  • done changes as per your suggestion.. but now also its avoiding if condition Commented Jan 14, 2015 at 9:47
  • Did you tried with a file that you are 100% that doesn't exist? Commented Jan 14, 2015 at 9:48
  • Can you, in a different file, do something like this: readfile('[path that was echoed on the other code]');? Commented Jan 14, 2015 at 9:51

4 Answers 4

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; } ?> 
Sign up to request clarification or add additional context in comments.

5 Comments

it showing /home/servername/public_html/my-site.com/assets/images-products/medium/imagenam‌​e.jpg
thanks, but it also didnt worked it always avoids if condition even if the image is available..
what does $image_file_path and $image_name_with_path prints
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
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
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'; 

Comments

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.

Comments

0

Use like this.

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

This is works for me in CI.

1 Comment

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation 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.