Skip to content

Instantly share code, notes, and snippets.

@mytory
Created April 21, 2016 08:17
Show Gist options
  • Save mytory/92369219da87edf851e195775fe64264 to your computer and use it in GitHub Desktop.
Save mytory/92369219da87edf851e195775fe64264 to your computer and use it in GitHub Desktop.
Download file with specific name in all browser.
/**
* Download file from path and mimetype.
*
* @param $path
* @param $mimetype
* @param null $filename
*/
function download_file ($path, $mimetype, $filename = NULL) {
if (empty($filename)) {
$filename = pathinfo($path, PATHINFO_BASENAME);
}
if (!strstr($_SERVER['HTTP_USER_AGENT'], 'Firefox')) {
$filename = rawurlencode($filename);
}
header('cache-control: no-cache');
header("Content-Type: $mimetype");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Length: " . filesize($path));
readfile($path);
}
/**
* Download text file from string.
*
* @param $filename
* @param $string
*/
function download_txt($filename, $string){
header('cache-control: no-cache');
header('Content-Type: text/plain');
header("Content-Disposition: attachment; filename={$filename}");
header("Content-Length: " . strlen($string));
echo $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment