Tuesday, October 9, 2018

How to download static docx file using php[Solved]

hey Guys, After lot of R&D I found the solution to download docx file using PHP.
Below is the example code.
function downloadDocx(){
$file = 'bikash_university.docx'
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
echo "<h1>Content error</h1><p>The file does not exist!</p>";
}