I'm trying to open files with this function. For files *.pdf
and *.txt
works fine, but if the file is *.doc
or *.odt
when downloading and opening it, strange characters appear.
$filepath = '/var/download/prueba.doc';
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length:'.filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
exit();
}
What am I doing wrong? Is there any way to open any type of file once downloaded ?. I do not need to open them online, I need to be able to download them and once downloaded, I can read them on my PC.