Created
January 29, 2018 21:01
-
-
Save alexey-kar/1633cbe49fdeae1efb01922a8779215b to your computer and use it in GitHub Desktop.
Файл для скачивания файла, будет скачивать файл оставаясь на текущей странице. Вызывать так download.php?id=123
This file contains hidden or 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
<? | |
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php"); | |
function download_translit($filename) { | |
static $tbl = array( | |
'а'=>'a', 'б'=>'b', 'в'=>'v', 'г'=>'g', 'д'=>'d', 'е'=>'e', 'ж'=>'g', 'з'=>'z', | |
'и'=>'i', 'й'=>'y', 'к'=>'k', 'л'=>'l', 'м'=>'m', 'н'=>'n', 'о'=>'o', 'п'=>'p', | |
'р'=>'r', 'с'=>'s', 'т'=>'t', 'у'=>'u', 'ф'=>'f', 'ы'=>'y', 'э'=>'e', 'А'=>'A', | |
'Б'=>'B', 'В'=>'V', 'Г'=>'G', 'Д'=>'D', 'Е'=>'E', 'Ж'=>'G', 'З'=>'Z', 'И'=>'I', | |
'Й'=>'Y', 'К'=>'K', 'Л'=>'L', 'М'=>'M', 'Н'=>'N', 'О'=>'O', 'П'=>'P', 'Р'=>'R', | |
'С'=>'S', 'Т'=>'T', 'У'=>'U', 'Ф'=>'F', 'Ы'=>'Y', 'Э'=>'E', 'ё'=>"yo", 'х'=>"h", | |
'ц'=>"ts", 'ч'=>"ch", 'ш'=>"sh", 'щ'=>"shch", 'ь'=>"", 'ъ'=>"", 'ю'=>"yu", 'я'=>"ya", | |
'Ё'=>"YO", 'Х'=>"H", 'Ц'=>"TS", 'Ч'=>"CH", 'Ш'=>"SH", 'Щ'=>"SHCH", 'Ъ'=>"", 'Ь'=>"", | |
'Ю'=>"YU", 'Я'=>"YA", | |
'№'=>"", ' '=>"_", '«'=>"_", '»'=>"_", '-'=>"_", '/'=>"_", '"'=>"_", '('=>"_", ')'=>"_", | |
); | |
return preg_replace("/__+/","_", strtr(trim($filename), $tbl)); | |
} | |
if ($arFile = CFile::GetByID( IntVal($_GET['id']) )->Fetch()) { // if found file | |
$file_path = $_SERVER["DOCUMENT_ROOT"] . '/upload/'. $arFile["SUBDIR"].'/'.$arFile["FILE_NAME"]; | |
// Set headers | |
header("Cache-Control: public"); | |
header("Content-Description: File Transfer"); | |
header("Content-Disposition: attachment; filename=".download_translit($arFile["ORIGINAL_NAME"])); | |
header('Content-Type: application/octet-stream'); | |
header("Content-Transfer-Encoding: binary"); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate'); | |
header('Pragma: public'); | |
header('Content-Length: '.$arFile["FILE_SIZE"]); | |
// Read the file from disk | |
ob_clean(); | |
flush(); | |
readfile($file_path); | |
exit; | |
} else { | |
die('File not found'); | |
} | |
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_after.php");?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment