1

I would like to download a PDF via PHP from my server, which is also uploaded via PDF. Although it works with some files I get the following error message with individual files:

ERR_CONTENT_LENGTH_MISMATCH

This is my download function:

if($_GET['funktion'] == 'downloadallgemein')
 {

        $filename = basename($dateiname);
        $path = '/kunden/261105_71522/webseiten/dev.delst/uploads/'.$filename.''; // the file made available for download via this PHP file
        if (file_exists($path)) {
                $mm_type="application/octet-stream"; // modify accordingly to the file type of $path, but in most cases no need to do so

                header("Pragma: public");
                header("Expires: 0");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                header("Cache-Control: public");
                header("Content-Description: File Transfer");
                header("Content-Type: " . $mm_type);
                header("Content-Length: " .(string)(filesize($path)) );
                header('Content-Disposition: attachment; filename="'.basename($path).'"');
                header("Content-Transfer-Encoding: binary\n");

                readfile($path); // outputs the content of the file

                exit();
        }

        else
        echo'Datei wurde nicht auf dem Server gefunden';
 }  

If I remove header("Content-Length: " .(string)(filesize($path)) ); the download works, but the PDF file is then broken and can not be opened.

The path is correct and the file is located on the server. So the upload works..

What am I doing wrong?

Codehan25
  • 2,704
  • 10
  • 47
  • 94
  • Why are you type casting file size in string? Also this issue may be related with server settings. Maybe there's a execution time limit set on the server. Disable it with set_time_limit(0); (if it's allowed). – Sehdev Jun 12 '17 at 07:17
  • If I disable the timelimit I still have the same behavior. If I remove the string-cast I also have the same error message.. – Codehan25 Jun 12 '17 at 07:31
  • Try to send smaller files and check if they are sent correctly. Inspect the headers that the server sends. – Sehdev Jun 12 '17 at 07:47

1 Answers1

0

header('Content-Type: application/pdf');

header('Content-Disposition: attachment; filename="' . basename($filename) . '"');

header('Content-Length: ' . filesize($filename));

readfile($filename);

I used this headers for downloading pdf in my php file

Community
  • 1
  • 1
aenugula karthik
  • 339
  • 5
  • 19