Tried using HTTP meta types ?
All u have to do is to use the headxer function to set the correct type for what u are sending, i.e.
Header( "Content-type: image/jpeg");
for sending a simple jpeg file to the webclient.
Ofcourse i guess there could be the need of encoding the file with base 64 before u write it to the stdout with some method (i.e. something better than 'echo $bytearray').
check some scripts for base 64 encoding, i know i've seen one out there on the net.