CodeIgniter Forums
[solved] accessing images in the view (strange behaviour) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter Archived (from EllisLab) (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: [solved] accessing images in the view (strange behaviour) (/thread-24170.html)



[solved] accessing images in the view (strange behaviour) - El Forum - 11-02-2009

[eluser]Unknown[/eluser]
Hello everyone,

Long time reader, first time poster. I've got a very strange problem. Maybe you can help.

In a view I try to load a file (previously uploaded). I check if it exists first and if it does I then display it. Check the code.

Code:
<?php if (file_exists(base_url() . 'img/image.jpg')) : ?>
    <img src="&lt;?php echo base_url() . 'img/image.jpg'; ?&gt;" />
&lt;?php endif ; ?&gt;

The problem is that the file_exists() function always returns FALSE even if I know for sure that given file does exist. For example this:

Code:
&lt;?php if (file_exists(base_url() . 'img/image.jpg')) : ?&gt;
    <h2>file found:</h2>
    <img src="&lt;?php echo base_url() . 'img/image.jpg'; ?&gt;" />
&lt;?php else : ?&gt;
    <h2>file NOT found:</h2>
    <img src="&lt;?php echo base_url() . 'img/image.jpg'; ?&gt;" />
&lt;?php endif ; ?&gt;

will display "file NOT found:" and draw the image anyway. I'm confused, please help. The /img/ catalog is already added to my .htaccess file.


[solved] accessing images in the view (strange behaviour) - El Forum - 11-02-2009

[eluser]BrianDHall[/eluser]
Your problem is with file_exists(): http://php.net/manual/en/function.file-exists.php

It expects a relative path to the file on your system, not an HTTP:// style url - which is what base_url is giving you.

I believe you can try getting rid of the base_url portion and just feed it the image path of img/image.jpg, as it is all relative to the front-controller index.php file unless specified otherwise.


[solved] accessing images in the view (strange behaviour) - El Forum - 11-02-2009

[eluser]Unknown[/eluser]
Thank you, works like a charm.