Created
April 21, 2016 08:17
-
-
Save mytory/92369219da87edf851e195775fe64264 to your computer and use it in GitHub Desktop.
Download file with specific name in all browser.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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