Extra space at beginning of downloaded image
Vinodkumar's answer didn't solved my issue, but it pointed me in the right direction.

I still haven't found the extra space, but as Drupal already used ob_start() in bootstrap.inc, I can now call ob_clean() to ensure no extra characters are shown:

&lt;?php

// ...extra code to populate $path and $filename

ob_clean();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename='.$filename);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($path));
readfile($path);
exit;