Forum Moderators: coopster

Message Too Old, No Replies

PHP images with fread

Problem with fread<ing> images and outputing on IIS

         

dldeskins

5:15 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Hello all,

I am having a problem with fread (or any other method) displaying an image on a Windows system (not my choice).

My script basically gets an image name from the db, reads it and displays it. I am using this to obscure the real name of the image and give it a new image name. The script works on my development box but doesn't on the production box... it only outputs garbage (like you would open an image in notepad).

I have checked and double checked usual culprits (magic quotes, etc.). The same version of PHP is on the dev and prod boxes.

here is my code:


if(!is_null($image) && file_exists($image))
{
$contents = '';
$size = getimagesize($image);
$handle = fopen($image, "rb");
while (!feof($handle)) {
$contents .= fread($handle, 1024);
}
fclose($handle);
$extension = strtolower(substr(strrchr($image, "."), 1));
}
header("Cache-Control: public");
header ("Content-type: image/jpg");
header("Content-Disposition: inline; filename=\"". $_GET['part'] . "\"");
header('Content-Length: ' . filesize($image));
print $contents;

BTW, $size here was just to check to see if the image functions worked, and they do. However, imagejpeg gives me the same output as the code above.

Any Suggestions?

Thanks

joelgreen

5:50 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Try setting

Content-Disposition: attachment

And if that does not work, maybe even header('Content-type: application/force-download');

dldeskins

5:55 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



I tried that, just to see if the image would download intact... the image was still corrupted. I want to display it inline, as an image in a page, so it has to be inline.

whoisgregg

6:01 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's an example of $_GET['part']? Perhaps, and this is just a random guess, the browser cares about the filename ending and $_GET['part'] doesn't include one?

header("Content-Disposition: inline; filename=\"". $_GET['part'] . ".jpg\""); 

Added Otherwise, I would recommend looking at the return headers from your development box and your production box and seeing what is different.

dldeskins

6:44 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Greg and Joel,

Thanks for your help, but I found it myself. Isn't that the way that it usually is, start talking and you figure it out?

The problem was that I was including a class file. At the end of the class file, I had a closing "?>", but also an extra line (carriage return). This file was different from the on on my dev box. By doing this, I was sending a header before the headers in my code. There was no error because the errors were turned off.

Here is the url:
<snip>

Thanks again,
Don

[edited by: eelixduppy at 7:45 pm (utc) on Oct. 3, 2007]
[edit reason] no urls, please [/edit]

whoisgregg

12:11 am on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it sorted, dldeskins. :)