Ivan
Ivan

Reputation: 2481

Force file download with PHP: corrupted files, have tried several solutions

This has been posted, but I've tried lot of solutions found on SO and more (like this: http://davidwalsh.name/php-force-download)

I basically have this:

{       
    $filePath = '../public/myfile.png';
    $fileName = basename($filePath);
    $fileSize = filesize($filePath);


    if (!is_file) {
        die("File not found");
    } else {                        
        header("Content-Description: File Transfer");
        header('Content-Type: application/octet-stream');
        header("Content-Disposition: attachment; filename= " . $fileName);
        header("Content-Transfer-Encoding: binary");
        readfile($filePath);
    }
}

Files are recognized and downloaded, but .PNGs are empty and .DOCs are corrupted (and Word asks me to fix the file, then it's ok). I have tried also PDFs, and no problem with that.

I trues to put all sort of options (Pragma, Cache-Control, Expires, Content-Length, etc.), still downloaded files but corrupted in some way...

Did you ever had my problem? Please consider I'm on IIS 7.5

Thanks in advance

Upvotes: 0

Views: 3112

Answers (1)

powtac
powtac

Reputation: 41080

Open the downloaded files with a plain text editor like Notepad++. At the top you will find a PHP Error notice, it will tell you what's going wrong.

The error is probably "session already send". Then add ob_start(); at the beginning of your script.

Upvotes: 8

Related Questions