I’m using the following code to allow users to download files from my hosted website. It works, but it’s not passing the filesize to a browser. I’ve tried both:
header('Content-Length: ’ . filesize($filename));
and:
$size=filesize($filename)
header('Content-Length: ’ . $size);
Here’s the full code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
function usdloader($filenumber=NULL){
require '../../php/PDO_Connection_Select.php';
if (!$pdo = PDOConnect("foxclone_data")) {
exit("unable to connect to database"); }
$test = $pdo->query("SELECT `filename` FROM `files` WHERE `id` = $filenumber");
$filename = $test->fetchColumn();
if (!file_exists($filename)) {
exit("Requested file ($file) does not exist");
}
else {
$file_path = './' . $filename;
$size = filesize($filename);
header('Content-Description: File Transfer');
header('Content-Type: ' . mime_content_type($filename));
header('Content-Disposition: attachment; filename='.$filename);
header('Expires: 0');
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile($filename); // Download the file
flush();
}
}
usdloader($_GET["f"]);
What am I doing wrong?
Thanks in advance