I am looking for just one php script to download files of any extension in particular doc, docx, pdf, jpg, png, gif.
Either the file will open in a browser or a dialog comes on to save the file
I searched through the internet and got different solution for different extension. Here are 3 examples:
// This is only good only for pdf
$file = 'path/testfile.pdf';
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));
readfile($file);
exit;
}
//ok only for pdf, docx by changing header Content-type:application
$file = 'testfile.docx';
$filename = 'path/testfile.docx';
header("Content-type:application/docx");
header("Content-Disposition:attachment;filename=".$file."");
readfile($filename);
// saves image file but microsoft office apps, pdf app, etc. cannot open file
$file = 'path/testfile.jpg';
$filename = 'testfile.jpg';
header('Content-type: application/jpg');
header('Content-Disposition: inline; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
@readfile($file);
I need just one script to satisfy all types of files