views:

64

answers:

3

Hello,

I am using the readfile function to read image files. It works in most cases except when an image is located in a directory with htaccess file.

Here is an example:

header("Content-type: image/jpeg");
ob_clean(); // clean output buffer
flush(); // flush output buffer 
readfile("http://127.0.0.1/WebProjects/project1/data/media/images/original/7.jpg");
exit;

The htaccess is located in the http://127.0.0.1/WebProjects/project1/ directory and looks like this:

RewriteEngine On

RewriteRule ^\.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^public/.*$ /public/index.php [NC,L]

Any way how to get around this? The htaccess file must be there because the web application in that folder needs it to work.

+3  A: 

Why not read the file directly from the filesystem instead of going through the web server?

readfile("/srv/WebProjects/project1/data/media/images/original/7.jpg");
Ignacio Vazquez-Abrams
Because that doesn't work either. I tried that as well and all I get is echoed directory path in web browser, not the actual image.
Richard Knop
Then something else is wrong. Are you sure the image isn't corrupt?
Ignacio Vazquez-Abrams
Yes. I can open the image in Paint, Mozilla, Gimp, Photoshop. It is ok.
Richard Knop
Why did this start working? did my file permissions hint really answer your question?
hopeseekr
A: 

RewriteCond %{REMOTE_ADDR}="127.0.0.1"

Use REMOTE_ADDR as a new RewriteCond before others to skip them. Since you exec code is local it will also be from 127.0.0.1 and should go through neatly.

thevikas
A: 

What's more than likely is that your images have read permissions for apache but no read permissions for whoever PHP runs as.

Try setting the permissions of the image to 0644.

hopeseekr
Why the down vote?! No valid permissions will result in a very similar condition.
hopeseekr