Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6886373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:45:04+00:00 2026-05-27T05:45:04+00:00

PHP Version 5.2.17 I have the following PHP script for downloading a binary file:

  • 0

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:

http://myserver/cur/downloads/test.php?get=FooBar_Setup.exe

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:45:05+00:00Added an answer on May 27, 2026 at 5:45 am

    You most likely have an extra blank line after the closing ?>, which is being echoed with the data. The closing ?> is always optional at the end of a PHP file. Leaving it out is a good practice to prevent this sort of problem.

    The reason the Content-Length changes is because the HTTP server (or PHP?) is ignoring it and adding a new one to match the actual data response before it gets sent it to the browser, so I think you can just leave that out. (If you do manage to send data with the wrong Content-Length, I’ve found browsers can act very weird.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My server have default php version of 4, so when I run some script
i have following script in php to login to mysql $db_host=localhost; $db_user=root; $db_pass=123; $dbc=mysql_connect($db_host,$db_user,$db_pass)
I have been trying to get slimv (http://www.vim.org/scripts/script.php?script_id=2531) working for a while now, but
I have an events calendar written in PHP, with the following pagination script that
I have the following code in a PHP file called via Ajax (note -
I have a weird issue with a php redirect script that does the following:
Does python have a function like call_user_func() in PHP? PHP Version: call_user_func(array($object,$methodName),$parameters) How do
I know in PHP, it sends the X-Powered-By header to have the PHP version.
We have a PHP project that we would like to version control. Right now
I have developed a PHP script that uses $_REQUEST[] superglobal. A typical client request

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.