views:

26

answers:

2

I'm attempting to force a download of an image that is in a directory above my website root. The download happens ok, and the correct filename is saved. However, the end-file is not a valid image and will not open or display properly. Here's my code:

        $photograph = new ViewPhotograph($photograph_id);
        $photograph->setPhotographVars();

        $file = $photograph->getPath('small');
        $filename = '1.jpg';

        header('Content-Description: File Transfer');
        header('Content-Type: image/jpg');
        header('Content-Disposition: attachment; filename=' . $filename);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
        exit;
A: 

Perhaps there's a PHP warning corrupting the stream.

Comment out the headers and see if you see a warning. If you do, fix it. (note that you shouldn't have display_errors turned on production servers).

Artefacto
When I comment out the headers, I receive the notice: Notice: ob_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete. in C:\xampp\htdocs\invivid\download.php on line 35 and a bunch of binary on the browser window
ThinkingInBits
@Thin Remove `ob_` calls completely, together with `flush`. I don't know why you put them there.
Artefacto
+1  A: 

do a ob_start before calling ob_clean()

once something is outputed by using echo or similar, you can't get rid of this, except, when you start a buffer before

xXx
before the headers?
ThinkingInBits
Wooo... worked. Thanks!
ThinkingInBits
maybe at the beginning of the file, to catch all errors or outputs
xXx