-
-
Save loburets/ae941fdb3904807c0613 to your computer and use it in GitHub Desktop.
php file downloading
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
<?php | |
function file_force_download($file) { | |
if (file_exists($file)) { | |
// сбрасываем буфер вывода PHP, чтобы избежать переполнения памяти выделенной под скрипт | |
// если этого не сделать файл будет читаться в память полностью! | |
if (ob_get_level()) { | |
ob_end_clean(); | |
} | |
// заставляем браузер показать окно сохранения файла | |
header('Content-Description: File Transfer'); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename=' . basename($file)); | |
header('Content-Transfer-Encoding: binary'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate'); | |
header('Pragma: public'); | |
header('Content-Length: ' . filesize($file)); | |
// читаем файл и отправляем его пользователю | |
readfile($file); | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment