EIOS/downloadfile.php

39 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2023-12-28 15:39:22 +03:00
<?php
//downloadfile.php?
if(isset($_GET['dwfile'])){
$dwfile = str_replace('..','',$_GET['dwfile']);
$dwfile = str_replace('/upload/','',$dwfile);
if(file_exists('./upload/'.$dwfile)){
$dwfile = './upload/'.$dwfile;
file_force_download($dwfile);
}else{
//echo '0';
}
}
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));
// читаем файл и отправляем его пользователю
if ($fd = fopen($file, 'rb')) {
while (!feof($fd)) {
print fread($fd, 1024);
}
fclose($fd);
}
exit;
}
}