Skip to content

Instantly share code, notes, and snippets.

@erlangparasu
Created February 21, 2017 09:08
Show Gist options
  • Save erlangparasu/b1fcf57d9a002afae9084b70ed311015 to your computer and use it in GitHub Desktop.
Save erlangparasu/b1fcf57d9a002afae9084b70ed311015 to your computer and use it in GitHub Desktop.
<?php
//
$filepath = 'file.txt';
/**
"htm" => "text/html",
"exe" => "application/octet-stream",
"zip" => "application/zip",
"doc" => "application/msword",
"jpg" => "image/jpg",
"php" => "text/plain",
"xls" => "application/vnd.ms-excel",
"ppt" => "application/vnd.ms-powerpoint",
"gif" => "image/gif",
"pdf" => "application/pdf",
"txt" => "text/plain",
"html"=> "text/html",
"png" => "image/png",
"jpeg"=> "image/jpg"
*/
$finfo = finfo_open(FILEINFO_MIME_TYPE);
header('content-type:' . finfo_file($finfo, $filepath));
// exe --> binary
$finfo = finfo_open(FILEINFO_MIME_ENCODING);
header('content-transfer-encoding:' . finfo_file($finfo, $filepath));
//
header('content-disposition:attachment;filename="' . basename($filepath) . '"');
//
readfile($filepath);
//
exit();
// ref:
// http://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment