PHP Force Download PDF Getts Corrupted in Opera Mini and Defaulot Android Browser

I have implemented a PHP code that authenticates a user and then forces the pdf download using readfile().

The code works well in desktop in all browsers (Chrome, mozilla and IE).

Read More

In mobile smartphones (Android), the script works only in three browsers; which are, mozilla,chrome and UC browser. In Opera mini and the default android browser the PDF is corrupted and when I view the corrupted files in notepad, I see only the html code.

How can I overcome this problem in Opera Mini and the default android browser?

Of course the document is located outside public_html in cpanel hence the need to force download.

Here is my code;

<?php
$file = "/home/mammitme/deeq/deeq_file.pdf"; //This simply specifies the path of the file to be read.
$fileName = explode("/", $file);
$fileName = end($fileName);

header('Content-Disposition: attachment; filename="'.$fileName.'"'); //This tells the browser the name to use in the download.
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: application/pdf;");
ob_clean ();
readfile("$file");`

Related posts