Nikk
Nikk

Reputation: 7889

Passing an image through PHP

I am using the following code trying to pass an image through my script, however all that happens is an html file gets downloaded instead.

This is a script for email tracking, at first I am updating a DB, then I actually move on to passing the image which is where I get the problem.

The html file that downloads, contains the following text GIF89a€ÿÿÿ!ÿ XMP DataXMP ÿþýüûúùø÷öõôóòñðïîíìëêéèçæåäãâáàßÞÝÜÛÚÙØ×ÖÕÔÓÒÑÐÏÎÍÌËÊÉÈÇÆÅÄÃÂÁÀ¿¾½¼»º¹¸·¶µ´``³²±°¯®­¬«ª©¨§¦¥¤£¢¡ Ÿžœ›š™˜—–•”“’‘ŽŒ‹Š‰ˆ‡†…„ƒ‚€~}|{zyxwvutsrqponmlkjihgfedcba``_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!  !ù,D;

This is my PHP

$graphic_http = 'blank.gif';
$filesize = filesize( 'blank.gif' );

    header( 'Pragma: public' );
    header( 'Expires: 0' );
    header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
    header( 'Cache-Control: private',false );
    header( 'Content-Disposition: attachment; filename="image.gif"' );
    header( 'Content-Transfer-Encoding: binary' );
    header( 'Content-Length: '.$filesize );
    readfile( $graphic_http );

Can anyone please point out what am I doing wrong?

Thanks

Upvotes: 1

Views: 650

Answers (2)

Geoff Atkins
Geoff Atkins

Reputation: 1703

You're not telling the browser what the file type is so it's not handling it as a GIF image, but as text.

Include the following line:

header ("Content-Type: image/gif");

//Edit
To prevent the file from being downloaded and simply opened in the browser, remove the line:
header( 'Content-Disposition: attachment; filename="image.gif"' );

This is what tells the browser to download the file rather than simply show it.

Upvotes: 4

Marcos P&#233;rez Gude
Marcos P&#233;rez Gude

Reputation: 22158

You need to put content-type, and if is for showing and not for download, you need to remove content-attachment:

header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private',false );
header('Content-Type: image/gif');
// header( 'Content-Disposition: attachment; filename="image.gif"' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Content-Length: '.$filesize );
readfile( $graphic_http );

Upvotes: 2

Related Questions