I am trying to write code to allow the download of files from a website to the user's computer. The file that downloads is truncated to 4k and is therefore not readable. What am I doing wrong? It is happening with different file types. The following is the code:
<?php
// block any attempt to the filesystem
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
} else {
$filename = NULL;
}
$err = "<p>Sorry, the file you are requesting is unavailable.</p>";
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
} else {
$path = 'downloads/'.$filename;
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header('Content-Type: image/jpeg');
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file can´t be opened
$file = fopen($path, ‘rb’);
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
}
?>
fpassthru truncating file.
Moderator: General Moderators