I’ve written a code in php, which is used in downloading link like https://example.com/dl.php&file=test.zip. Here the file dl.php checks the HTTP refer, if it is allowed then it download the file from “up” directory. it gets the file name from “file” URL parameter and check the file in “up” directory and download it.
Everything is working perfectly, but when the file is being downloaded it doesn’t show File size and Resume option. I am using WHM/cPanel with Engintron. Its fresh server installation. Same script works perfectly on AApanel but doesn’t show file size here on WHM. Any help would be highly appreciated.
here is my code for dl.php
<?php
// Allowed referring domains
$allowedDomains = [
'https://reidostorrents.net.br/',
'https://programastorrent.com.br/',
'https://fullprogramindir.com.tr/',
'https://jogosbaixar.net/',
'https://gustavortech.org/',
'https://ativadorfiles.com/',
'https://downloadcomputerprograms.org/',
'https://baixakiprogramas.com.br/',
'https://computergamesegypt.com/'
];
// Get the referring domain from the HTTP Referer header
$referer = $_SERVER['HTTP_REFERER'];
// Check if the referring domain is in the allowed domains list
if (in_array($referer, $allowedDomains) || startsWith($referer, 'https://reidostorrents.net.br')) {
// Allow access to the specified file in the directory
$directory = __DIR__ . '/up'; // Ensure you are pointing to the correct directory
// Get the requested file from the query parameter
$requestedFile = isset($_GET['file']) ? $_GET['file'] : '';
// Validate the requested file and check for allowed extensions
$allowedExtensions = ['zip', 'rar'];
$fileExtension = pathinfo($requestedFile, PATHINFO_EXTENSION);
$file = $directory . '/' . basename($requestedFile);
if (is_file($file) && in_array($fileExtension, $allowedExtensions)) {
// If the file exists and has an allowed extension, handle resume download
$fileSize = filesize($file);
// Handle range requests
$range = isset($_SERVER['HTTP_RANGE']) ? $_SERVER['HTTP_RANGE'] : '';
if ($range) {
list($unit, $range) = explode('=', $range, 2);
if ($unit == 'bytes') {
list($start, $end) = explode('-', $range, 2);
$start = (int)$start;
$end = (isset($end) && is_numeric($end)) ? (int)$end : $fileSize - 1;
if ($start > $end || $start >= $fileSize || $end >= $fileSize) {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
header("Content-Range: bytes */$fileSize");
exit;
}
} else {
header('HTTP/1.1 416 Requested Range Not Satisfiable');
exit;
}
} else {
$start = 0;
$end = $fileSize - 1;
}
$length = $end - $start + 1;
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Length: ' . $length);
header('Accept-Ranges: bytes');
if ($range) {
header('HTTP/1.1 206 Partial Content');
header("Content-Range: bytes $start-$end/$fileSize");
} else {
header('HTTP/1.1 200 OK');
}
$fileHandle = fopen($file, 'rb');
fseek($fileHandle, $start);
$bufferSize = 8192;
while (!feof($fileHandle) && ($pos = ftell($fileHandle)) <= $end) {
if ($pos + $bufferSize > $end) {
$bufferSize = $end - $pos + 1;
}
echo fread($fileHandle, $bufferSize);
ob_flush();
flush();
}
fclose($fileHandle);
} else {
// Invalid or non-existent file or extension
http_response_code(404);
echo 'No File Found.';
}
} else {
// Show forbidden error
http_response_code(403);
header('Location: https://reidostorrents.net.br');
exit;
}
// Helper function to check if a string starts with another string
function startsWith($haystack, $needle) {
return strpos($haystack, $needle) === 0;
}
?>
and here is .htaccess file content
# Enable the Rewrite Engine
RewriteEngine On
# Allow access to all files
<Files *>
Order Allow,Deny
Allow from all
</Files>
# Disable directory listing if desired
Options -Indexes