Serving Image Files Not Working in IE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wallacer
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:16 pm

Serving Image Files Not Working in IE

Post by wallacer »

I'm using a php script I've called file.php to serve files to my users. I'm using this method to restrict direct access to the files.
This is a simplified version of how I manage this task.

Code: Select all

 
$file = $_GET['file'];
$filename = end(explode('/',$file));
 
header("Content-Type: $conType");
header("Content-Disposition: inline; filename=\"".basename($filename)."\"");
readfile($file);
 
$conType is a string of the form "image/xxxx" or "application/xxxx" and is the appropriate MIME type as determined by the file extension.

Anyways, this is a pretty standard way of serving files using php as far as I know. It works great too! In Firefox..... no surprise here, it doesn't work in IE!? Well i shouldn't say it doesn't work. PDF's seem to work fine. Its really just images that don't seem to work.

So I've tried a bunch of other methods of performing the same basic task, and I just can't seem to get IE to recognize that file.php is returning an image.

If the file is an image show it like so: <img src="thepathto/file.php?file=thepathto/someImg.jpg" />

for pdf's and other app files I use embed.

If anyone could help me get this working in IE I would be very grateful!
Thanks!
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Serving Image Files Not Working in IE

Post by Darhazer »

:offtopic:
If the purpose of this line is to get the filename from a file path

Code: Select all

$filename = end(explode('/',$file));
Than take a look at the basename function 8)

Edit: I just saw you are using it later in the code...

OK, what's the exact file type you are using and do you know if this affects only IE 6 or later versions as well?
Try without the content-disposition header, I never use such when I'm embeding files?
wallacer
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:16 pm

Re: Serving Image Files Not Working in IE

Post by wallacer »

my later use of basename is pretty redundant.... since I think basename is basically the same as end(explode( '/', $path));

But... back on topic. It doesn't work in IE 6 - 8.

it doesn't work when I remove the content disposition header.

The filetypes that don't display that I've tested are jpg, png, gif

Though I'm pretty sure all image types fail. Like I said before however, IE figures out pdf just fine...

thanks for the speedy response! :D
Last edited by wallacer on Mon Jul 20, 2009 3:16 pm, edited 1 time in total.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: Serving Image Files Not Working in IE

Post by spider.nick »

wallacer wrote:I'm using a php script I've called file.php to serve files to my users. I'm using this method to restrict direct access to the files.
This is a simplified version of how I manage this task.

Code: Select all

 
$file = $_GET['file'];
$filename = end(explode('/',$file));
 
header("Content-Type: $conType");
header("Content-Disposition: inline; filename=\"".basename($filename)."\"");
readfile($file);
 
$conType is a string of the form "image/xxxx" or "application/xxxx" and is the appropriate MIME type as determined by the file extension.

Anyways, this is a pretty standard way of serving files using php as far as I know. It works great too! In Firefox..... no surprise here, it doesn't work in IE!? Well i shouldn't say it doesn't work. PDF's seem to work fine. Its really just images that don't seem to work.

So I've tried a bunch of other methods of performing the same basic task, and I just can't seem to get IE to recognize that file.php is returning an image.

If the file is an image show it like so: <img src="thepathto/file.php?file=thepathto/someImg.jpg" />

for pdf's and other app files I use embed.

If anyone could help me get this working in IE I would be very grateful!
Thanks!
Where are you setting $conType?

Nick
wallacer
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:16 pm

Re: Serving Image Files Not Working in IE

Post by wallacer »

Where are you setting $conType?

Nick

I did note that that was a "simplified version". I set conType earlier.

Code: Select all

$ext = strtolower(end(explode('.',$filename)));
 
$known_mime_types=array(
    "pdf" => "application/pdf",
    "txt" => "text/plain",
    "html" => "text/html",
    "htm" => "text/html",
    "exe" => "application/octet-stream",
    "zip" => "application/zip",
    "doc" => "application/msword",
    "xls" => "application/vnd.ms-excel",
    "xlsx" => "application/vnd.ms-excel",
    "ppt" => "application/vnd.ms-powerpoint",
    "gif" => "image/gif",
    "png" => "image/png",
    "jpeg"=> "image/jpeg",
    "jpg" =>  "image/jpeg",
    "bmp" => "image/bmp",
    "php" => "text/plain"
 );
 
$conType = $known_mime_types[$ext];
that's what I use to set conType shortly before I call header. Regardless, even if I just set conType to "image/jpeg", and use this to open a jpeg file, IE doesn't work, but firefox does.
spider.nick
Forum Commoner
Posts: 72
Joined: Wed Jul 15, 2009 12:22 pm
Location: Overland Park, KS

Re: Serving Image Files Not Working in IE

Post by spider.nick »

Have you tried putting:

Code: Select all

exit;
After your readfile?

Nick
wallacer
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:16 pm

Re: Serving Image Files Not Working in IE

Post by wallacer »

sorry for the delay... lunch meeting.

Bugger! I was really hoping your exit command would be the easiest fix imaginable there! Unfortunately no... IE still doesn't want to show an image when it comes from my file.php

I should add that I've tried other headers including:

header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Transfer-Encoding: binary');
header("Accept-Ranges: bytes");

header("Content-Length: ".filesize($file));

in various combinations. I really just think that for whatever reason IE is not accepting the MIME type of image/xxxx and is still trying to display it as text/html or something...

Any more insight would be much appreciated, and thanks for the efforts so far!! :D
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Serving Image Files Not Working in IE

Post by Darhazer »

Just setuped this simple test:

Code: Select all

<?php
header('Content-type: image/gif');
readfile('2344_orig.gif');
?>
IE 8 loads the image.... http://it-light.net/test.php

Do you have included files?
Maybe you are using output buffering with gzip?
Last edited by Darhazer on Mon Jul 20, 2009 4:53 pm, edited 1 time in total.
wallacer
Forum Newbie
Posts: 5
Joined: Mon Jul 20, 2009 2:16 pm

Re: Serving Image Files Not Working in IE

Post by wallacer »

Holy crap.... OK.. I've figured it out!

I've spent hours and hours :banghead: on this... It's part of a fairly large system (hence the dumbing it down for examples part).

SO. Here it is: IE doesn't like a "width" parameter in the img tag when you load an image using a php script like this. Firefox just ignores it, but IE gets all buggered up and ends up not displaying an image.

I used to have <img src="..." width="..." />

take out the width and you're good to go :D I'd imagine the height parameter will cause similar issues.

I hope this helps someone someday with the same issue as me. Thanks for the help guys!
Post Reply