Skip to content

Instantly share code, notes, and snippets.

@loburets
Created February 12, 2016 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loburets/ae941fdb3904807c0613 to your computer and use it in GitHub Desktop.
Save loburets/ae941fdb3904807c0613 to your computer and use it in GitHub Desktop.
php file downloading
<?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