I have these code for viewing file in my browser when user click on download button. These code is working fine for pdf,text,image file. But when i download word file it can't show the preview of word file like other files and also wheni downlaod the word file it shows me error. Here i upload the image of error.
$file = PATH.'/'.$File_Name;
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file);
header('Content-disposition: inline');
header('Pragma: public');
header('Expires: 0');
header('Content-Type:'.$Mime_Type);
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.basename($File_Name).'"');
header('Content-Disposition: inline;filename="'.$File_Name.'"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Length' . filesize($file));
ob_clean();
flush();
readfile($file);
The best way to render the docx format on the browser is by putting it on Google Drive and get an iframe tag. Please refer to How do I render a Word document (.doc, .docx) in the browser using JavaScript?