tags:

views:

64

answers:

2

when i click a button the browser force the image to download the code work it download the image with the right size but it's all black im using this code :

    // get attachment location
    $attachment_location = "files/image.jpg";

    if (file_exists($attachment_location)) {
        // attachment exists
        // send open/save jpg dialog to user
        header('Cache-Control: public'); // needed for i.e.
        header('Content-Type: application/jpg');
        header('Content-Disposition: attachment; filename="image.jpg"');
        readfile($attachment_location);
        die(); // stop execution of further script because we are only outputting the jpg
        }
    else {
        die('Error: File not found.');
    }
A: 

I tried your code on my test server. (PHP5.3, Apache2.2, Win7)
Works fine in both Firefox and IE8.

Are you sure the problem is not with your image, or with your own browser?
Which version of PHP are you using? What HTTP server? Which OS?
Which browser did you test this on?

Atli
my code works fine thanks guys
A: 

Is it possible you have other data still in the buffer? what happens if you use echo file_get_contents() instead of readfile? What happens if you call ob_clean(); flush(); before calling readfile?

Also do you have error_reporting truned on? Try turning it off before calling readfile or using any of the buffer operations i mentioned above.

prodigitalson