Hello,

I seem to be having a problem with my script. I can't seem to get it to download a file using fopen. readfile works great, but with files that exceed 4MB size readfile will not work. So to fix this I use fopen for files over 4MB. When it tries to download the file, I get an error saying that the source file cannot be read. I double checked the file its attempting to download and it has permissions 0644, and when transferred via FTP it works fine. Can anyone shed some light on why this is broken? (This just recently started happening, for a while the script worked great).

$filename = 'downloads/file.zip';
if (filesize($filename) > 4194304)
{
    $fp = fopen($filename, 'rb');
    while (!feof($fp))
    {
        echo fread($fp, 8192);
        flush();
     }
fclose($fp);
}
else
{
     readfile($filename);
}

Any help is appreciated. Thank you.

    ScopeXLL wrote:

    with files that exceed 4MB size readfile will not work.

    What does "will not work" mean? What error messages are being generated?

      bradgrafelman;10958779 wrote:

      What does "will not work" mean? What error messages are being generated?

      No errors are generated as I am using php header files and I have to set error_reporting to 0 otherwise it could conflict with the script. But basically it downloads files with readfile just fine with files smaller than 4MB, if I try to use readfile on a file larger than 4MB than it simply downloads a 0byte, corrupted file. This is why I am attempting to use fopen for files larger than 4MB, but I get an error generated by firefox when downloading saying The source file cannot be read. I checked the permissions and the file its attempting to download is at 0644. This is where I get stuck. Script works great for files under 4MB, past that I cannot get it do work.

        ScopeXLL wrote:

        I have to set error_reporting to 0 otherwise it could conflict with the script.

        Why would having a sensible error_reporting level "conflict" with your script?

          I set error_reporting(E_ALL); and got no PHP error. Instead I got a firefox error saying:

          Content Encoding Error
          The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.

          I checked my server and have zip, zlib, gzip installed and running the latest version. I am not sure why my server is acting like this, but it only seems to happen with larger files?

            Try logging the errors instead of displaying them. Otherwise, you'll have to view the raw HTTP response from the server rather than expecting your browser to understand what's going on.

              Write a Reply...