PHP Version 5.2.17
I have the following PHP script for downloading a binary file:
<?php
$download_dir = '.';
$download_file = $_GET['get'];
$path = $download_dir.'/'.$download_file;
if(file_exists($path))
{
$size = filesize($path);
header('Content-Type: application/x-download');
header('Content-Disposition: attachment; filename='.$download_file);
header('Content-Length: '.$size);
header('Content-Transfer-Encoding: binary');
readfile($path);
}else{
echo "<font face=$textfont size=3>";
echo "<center><br><br>The file [<b>$download_file</b>] is not available for download.<br>";
}
?>
URL used to call this script:
Downloading works but the downloaded file has an additional byte (0x0a) appended.
The headers of the response also show a Content-Length that is one byte larger than the size of the requested file:
HTTP/1.1 200 OK
Date: Mon, 05 Dec 2011 10:29:32 GMT
Server: Apache/2.2
Content-Disposition: attachment; filename=FooBar_Setup.exe
Content-Length: 1417689
Content-Transfer-Encoding: binary
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/x-download
The size of the file on the server is 1417688 bytes.
I have verified that filesize($path)
returns the correct file size. It seems that PHP alters the Content-Length header with filesize($path)+1
.
I see this behavior with two different files.
How can I get prevend PHP from appending 0x0a to the downloaded binary files?