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 6054579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:10:07+00:00 2026-05-23T08:10:07+00:00

I have written a PHP page for downloading files from the server. The name

  • 0

I have written a PHP page for downloading files from the server. The name of the file is passed as a GET variable in the URL, and then the following code serves up the file for download:

$filepath = "/path/to/files";
$filename = $_GET['id'];

if( ! file_exists($filepath . "/" . $filename) )
{
    header("HTTP/1.1 404 Not Found");
    @session_destroy();
    exit(0);
}

$cmd = '/usr/bin/stat -c "%s" ' . $filepath . "/" . $filename;
$out = array();
$ret = 0;

exec( $cmd, $out, $ret );

header('Content-type: application/octet-stream');
header('Content-Length: ' . $out[0]);
header('Content-Disposition: attachment; filename="' . $filename . '"');

readfile($filepath . "/" . $filename);

NOTE: I am using the exec() call because most of the files are large (>2GB) and the larger files caused the filesize() and stat() functions to fail.

Anyway, this code works perfectly for almost all of the files. However, when a file is exactly 2 GB in size (2147483648 bytes), no headers are sent and the browser attempts to download the PHP page itself, which results in a empty file being saved called download.php.

Here’s what happened when I tested this with curl:

Test #1: Get a 1 GB file called bigfile1:

$ curl -v http://<SERVER>/download.php?id=bigfile1
* About to connect() to <SERVER> port 80 (#0)
*   Trying <IP_ADDRESS>... connected
* Connected to <SERVER> (<IP_ADDRESS>) port 80 (#0)
> GET /download.php?id=bigfile1 HTTP/1.1
> User-Agent: curl/7.18.2 (i486-pc-linux-gnu) libcurl/7.18.2 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.8 libssh2/0.18
> Host: <SERVER>
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Tue, 21 Jun 2011 19:10:06 GMT
< Server: Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.8c PHP/5.3.0
< X-Powered-By: PHP/5.3.0
< Set-Cookie: PHPSESSID=382769731f5e3782e3c1e3e14fc8ae71; path=/
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Content-Length: 1073741824
< Content-Disposition: attachment; filename="bigfile1"
< Content-Type: application/octet-stream
<
* Connection #0 to host <SERVER> left intact
* Closing connection #0

Test #2: Get a 2 GB file called bigfile2

$ curl -v http://<SERVER>/download.php?id=bigfile2
* About to connect() to <SERVER> port 80 (#0)
*   Trying <IP_ADDRESS>... connected
* Connected to <SERVER> (<IP_ADDRESS>) port 80 (#0)
> GET /download.php?id=bigfile2 HTTP/1.1
> User-Agent: curl/7.18.2 (i486-pc-linux-gnu) libcurl/7.18.2 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.8 libssh2/0.18
> Host: <SERVER>
> Accept: */*
>
* Empty reply from server
* Connection #0 to host <SERVER> left intact
curl: (52) Empty reply from server
* Closing connection #0

I have created test files which are 1 GB, 2 GB, 3 GB, 4 GB, and 5 GB in size. The 2 GB file is the only one that causes this behavior, but it happens consistently, and it seems to happen regardless of the client browser or OS. The server is running Debian GNU/Linux 4.0, Apache 2.2.4, and PHP 5.3.0.

Any ideas would be greatly appreciated. Thanks!

  • 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-23T08:10:08+00:00Added an answer on May 23, 2026 at 8:10 am

    On a 64-bit installation, I had no problems with the file size you mentioned using PHP 5.3.6. A 32-bit installation gave me:

    failed to open stream: Value too large for defined data type
    

    But that said, I would not even use readfile. Instead:

    header("X-Sendfile: $filepath/$filename");
    

    It requires mod_xsendfile be installed and configured.

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

Sidebar

Related Questions

I have written jQuery code, in files Main.html and ajax.php . The ajax.php file
I have written a php code which will get one id from database and
i am trying to implement pretty url. Everything i have written in PHP and
The situation is next: I have php file, which parses a web-page. on that
Let's say we have a page written in PHP. This page loads by it
I have page I have written in PHP. I am inserting a couple values
I have a bunch of CSS files that get loaded on every page (header,
I have a page written with php where, for some reason, all of the
I have a page written in PHP. I do not have access to the
I have written a small PHP file that gets the information that was posted

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.