The multipart/x-mixed-replace content type

Date: 08 Jan 2012
Author: Erik Dubbelboer

Looking at the new Google Analytics live view I noticed they use the multipart/x-mixed-replace content type to push live updates to the webbrowser.

Using this special Content-type you can replace the contents of a page. You can find this example here: dubbelboer.com/multipart.php.

<?

header('Content-type: multipart/x-mixed-replace; boundary=endofsection');

// Keep in mind that the empty line is important to separate the headers
// from the content.
echo 'Content-type: text/plain

After 5 seconds this will go away and a cat will appear...
--endofsection
';
flush(); // Don't forget to flush the content to the browser.


sleep(5);


echo 'Content-type: image/jpg

';

$stream = fopen('cat.jpg', 'rb');
fpassthru($stream);
fclose($stream);

echo '
--endofsection
';

It’s fun to experiment with but since it isn’t supported by IE it’s not really practical for real usage.

comments powered by Disqus