<?php
function
downloadFile(
$filePath
,
$readBuffer
= 1024,
$allowExt
= ['jpeg', 'jpg', 'peg', 'gif', 'zip', 'rar', 'txt'])
{
if
(!
is_file
(
$filePath
) && !
is_readable
(
$filePath
)) {
return
false;
}
$ext
=
strtolower
(
pathinfo
(
$filePath
, PATHINFO_EXTENSION));
if
(!in_array(
$ext
,
$allowExt
)) {
return
false;
}
header('Content-Type: application/octet-stream');
header('Accept-Ranges:bytes');
$fileSize
=
filesize
(
$filePath
);
header('Content-Length:' .
$fileSize
);
header('Content-Disposition:attachment;filename=' .
basename
(
$filePath
));
$handle
=
fopen
(
$filePath
, 'rb');
while
(!
feof
(
$handle
) ) {
echo
fread
(
$handle
,
$readBuffer
);
}
fclose(
$handle
);
exit
;
}