I've got an image, let's say in http://localhost/myapp/uploads/image.jpg
.
And i've got a script that pulls this image for display on the home page, like so:
<img src="<? echo base_url().'uploads/.$filename ; ?>
That eventually puts out the following HTML:
<img src="http://localhost/myapp/uploads/image.jpg">
Now, when I access the folder in windows explorer, I can clearly see the image there.
However, when trying to show it using the link as specified above - it throws a 404.
A few things:
base_url()
is set correctly."GET /myapp/uploads/image.jpg HTTP/1.1 " 404 1175
I'm confused as to what exactly went wrong in here.
So - The soultion proves to be simpler than i thought.
I opened up my .htaccess file (which cancels out the use of index.php) and changed:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
To:
RewriteEngine On
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Options -Indexes
Which solves the problem.
Apparently I used a .htaccess that prevents images from being shown....if somebody could explain exactly what these several lines of code do - I'd be grateful.
your <img>
tag doesn’t seem right:
<img src="<? echo base_url().'uploads/.$filename ; ?>
Shouldn’t it be:
<img src="<? echo base_url() . 'uploads/' . $filename ; ?>" />
And to avoid you going insane with issues like this again, I highly recommend avoiding template code format like this & instead do this:
$img_url = base_url() . 'uploads/' . $filename ;
echo "<img src=" . $img_url . " />";
I personally think using inline PHP code mixed with HTML is a massive debugging headache that should be avoided at all costs.
EDIT Also, what happens when you attempt to load it via the browser directly? or for that matter, when you are in the command line/terminal, what is the response you get when using:
curl -I http://localhost/myapp/uploads/image.jpg
That should return the full headers of the image and convey a 200
if the image retrieval was successful or a 404
if the image is not loaded.
And on top of that, when you say:
Now, when I access the folder in windows explorer, I can clearly see the image there.
You mean by just browsing the file system? You need to get the full URL as it would be via the browser. Perhaps your image has some uppercase/lowercase issues? Or maybe even a stray space at the beginning or end of the filename? But in general, this is a fairly straightforward issue to debug.
Should be 'uploads/'
<img src="<? echo base_url().'uploads/'.$filename ; ?>
instead of
<img src="<? echo base_url().'uploads/.$filename ; ?>
You are missing closing quote and bracket at the end, " />
Not 'uploads/.$filename
should be 'uploads/'.$filename
<img src="<? echo base_url().'uploads/'.$filename ; ?>"/>