0

code:

<?php $filename = FCPATH.'resources/img/college_logo/'.trim($fetch['college_name']).'.jpg'; if (file_exists($filename)) { ?> <img src="<?php echo $filename; ?>"> <?php } else { ?> <img src="<?php echo base_url(); ?>resources/img/college_logo/logo_not.jpg"> <?php } ?> 

enter image description here

I am new in codeigniter and I want to display image if it exists else display logo not found image but now it only display logo not found. while I echo $filename it show me image path when I put path in url then it show me image but here its not working for me. So, How can I fix this issue ? please help.

Thank You

8
  • 1
    $filename is an absolute HTTP URL? Commented Jun 15, 2017 at 8:16
  • 1
    make sure file_exists() needs to use a file path on the hard drive, not a URL Commented Jun 15, 2017 at 8:18
  • if I am using $filename ='localhost/collegescan_ci/resources/img/college_logo/…'; then it show me logo not found why ? Commented Jun 15, 2017 at 8:21
  • @omkara maybe it doesn't exist in that local path. Commented Jun 15, 2017 at 8:21
  • If you want to use URL's with file_exists you need to enable allow-url-fopen Commented Jun 15, 2017 at 8:23

1 Answer 1

1

Problem is because you are using file system path if file is found. You should use URL and not absolute system path to present image in browser.

<?php $filename = 'resources/img/college_logo/'.trim($fetch['college_name']).'.jpg'; if (file_exists(FCPATH.$filename)) { ?> <img src="<?php echo base_url($filename); ?>"> <?php } else { ?> <img src="<?php echo base_url('resources/img/college_logo/logo_not.jpg'); ?>"> <?php } ?> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Tpojka

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.