Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problems with MySQL and large file d/ls

    First off, thank you to those who were nice enough to help me in my last thread. This is sort of a continuation - even tho that problem was solved, a new one has popped up.

    I have a custom script that automatically generates a unique download key, and then gets saved to a MySQL database that I have created. This is to prevent customers from discovering the location of the actual download file.

    I have everything setup and working just fine. I go to my generatekey page, get a key (d/l link), c&p into a browser, and the file d/ls just fine - unless the file is too large. I am not sure what the tolerance is, but I can say that a file of only a few megs will d/l fine each and every time. A file over 250 MB will not - occasionally it d/ls fine, but more often then not, it doesn't. And the result varies. A 266 MB file I am currently testing with will wind up being 212 MB, 245 MB, 249 MB, 231 MB on four consecutive d/ls. Each of those d/ls is a unique key. I called my host to see if I was running in to some limit, but was informed that the limit is 1 GB/file, so that is not the issue. Breaking up the file into smaller chunks is not an option; the file is a .rar file that already is part of a larger file. It's 3 GB, which ultimately I want to break down into 6 500 MB files.

    So, I have no problem posting code, but I am not really sure what to post here. I will start with the end of my d/l.php:

    Code:
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($fakefilename) .'"');
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($realfilename));
    ob_clean();
    flush();
    readfile($realfilename);
    exit;
    }
    }
    }
    ?>
    Do not know if that is of any use, but again, not sure what would be. So, I do some searching around, and I find this:

    http://www.php.net/manual/en/function.header.php#86554

    --edit--- Hmmmm....for some reason, clicking that link does not bring you to the correct part of the page, but a c&p will. Here is what it says:

    --------------

    For large files (100+ MBs), I found that it is essential to flush the file content ASAP, otherwise the download dialog doesn't show until a long time or never.

    <?php
    header("Content-Disposition: attachment; filename=" . urlencode($file));
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Content-Description: File Transfer");
    header("Content-Length: " . filesize($file));
    flush(); // this doesn't really matter.

    $fp = fopen($file, "r");
    while (!feof($fp))
    {
    echo fread($fp, 65536);
    flush(); // this is essential for large downloads
    }
    fclose($fp);
    ?>


    --------------

    I tried playing with that code, but wasn't sure is I did everything I needed to. For ex., I changed:

    Code:
    header('Content-Length: ' . filesize($realfilename));
    ...but there are some other things I wasn't sure about. Like:

    Code:
    $fp = fopen($file, "r");
    Then the other thing is, at the top it says, "I found that it is essential to flush the file content ASAP, otherwise the download dialog doesn't show until a long time or never." Well, that sounds like it fixes a problem of the d/l dial not showing - which is not the problem I have. So I wonder that, even if I do get the code from this page 100% correct, if that is really the fix.

    Can anyone offer any suggestions as to where to look? Any advice will be greatly appreciated. Thank you!
    Last edited by riffwraith; 09-14-2012 at 02:18 AM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    It sounds to me like you have a mixture of issues going on.

    Sounds like your script is hitting the max_execution_time and then stopping.

    readfile() on large files isn't a good idea. It can have the same effect as making a process look like its frozen (in theory - I've not seen the php source) and your server might be terminating it automatically.

    It may also be that your php is encountering an error and printing these out in the file stream instead of the filestream itself. Only way to check that is (be warned this can be very cpu / memory intensive) to open the file up in notepad / notepad++ and look for any html output.

    My site is on a windows host and this sort of script is useless for me. Works great on my local setup but my host has a 2 minute execution limit (a real PITA) so any file downloads I tested always had pretty random sizes too. In short, you've a lot of testing to do.

    The way to debug scripts like this is to LOG things to a database table or a text file. If you're going to use a loop in your code (best idea) then you can log every loop to your table with things like the time, the current amount of data written out, the cycle number etc. That should get you on your way to working out what is wrong.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Aug 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply.

    Yes it has taken this long (been busy with other things), but the problem has been solved. Evidently, godaddy has some issues. I switched hosts, and now all d/ls of any size are fine - thanks again.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •