-
-
Save pokev25/9ef3b8fc401d4aa430c7ec7441c7e0a2 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