Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-14-2012, 02:14 AM   PM User | #1
riffwraith
New Coder

 
Join Date: Aug 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
riffwraith is an unknown quantity at this point
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..
riffwraith is offline   Reply With Quote
Old 09-14-2012, 10:26 AM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,665
Thanks: 45
Thanked 456 Times in 444 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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.
__________________
Please don't be rude: Put your php code in [php][/php] tags. It is a sticky topic at the top of the forum and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 10-18-2012, 03:23 AM   PM User | #3
riffwraith
New Coder

 
Join Date: Aug 2012
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
riffwraith is an unknown quantity at this point
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.
riffwraith is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:56 AM.


Advertisement
Log in to turn off these ads.