473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Why does download script never save the last chunk?

162 100+
I'm using the following script to download large file (>100Mb). It works well except that it seems to not ever save the final chunk. If the file is 149,499 on the server, it finishes its download at 145,996. Why? How do I get the last 2% or so to flush and complete the download? Thank much for your help.

FYI, this also happens the same on smaller files so its not stopping for time or file size issues.

Expand|Select|Wrap|Line Numbers
  1.  
  2. $path = "the/file/path.mp4";
  3.  
  4. $headsize = get_headers($path,1);
  5.  
  6. $ext = str_from_last_occurrence($_vars['filepath'],".");
  7.  
  8. if ($ext=="mp3") { $type = "audio"; }
  9. elseif ($ext=="mp4") { $type = "video"; }
  10.  
  11. function readfile_chunked($filename,$retbytes=true) {
  12.  
  13.     // Stream file
  14.     $handle = fopen($filename, 'rb');
  15.     $chunksize = 1*(1024*1024); // how many bytes per chunk
  16.     $buffer = '';
  17.     $cnt =0;
  18.  
  19.    if ($handle === false) {
  20.        return false;
  21.    }
  22.  
  23.    while (!feof($handle)) {
  24.        $buffer = fread($handle, $chunksize);
  25.        echo $buffer;
  26.        ob_flush();
  27.        flush();
  28.        if ($retbytes) {
  29.            $cnt += strlen($buffer);
  30.        }
  31.    }
  32.  
  33.    $status = fclose($handle);
  34.  
  35.    if ($retbytes && $status) {
  36.        return $cnt; // return num. bytes delivered like readfile() does.
  37.    }
  38.  
  39.    return $status;
  40.  
  41. }
  42.  
  43. header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
  44. header("Content-type: ".$type."/".$ext);
  45. header('Content-Length: ' . (string)($headsize['Content-Length']));
  46. header('Content-Disposition: attachment; filename="'.str_from_last_occurrence($_vars['filepath'],"/").'"');
  47. header("Content-Transfer-Encoding: binary");
  48.  
  49. readfile_chunked($path);
  50.  
  51. exit;
  52.  
Feb 9 '11 #1
9 2346
HaLo2FrEeEk
404 256MB
I'm using this exact same function and it works fine for me. I can download huge files just fine. Are the file you're downloading located on your own server? If so then don't bother getting the headers, that's just wasteful, use filesize() instead. Here's the download headers I'm setting, unmodified from my site's code, so they won't be plug n' play for you:

Expand|Select|Wrap|Line Numbers
  1. header("Cache-Control: ");
  2. header("Pragma: ");
  3. header("Content-type: application/octet-stream");
  4. header("Content-Disposition: attachment; filename=\"".$download."\"");
  5. header("Content-length:".(string)(filesize($path.$download)));
  6. readfile_chunked($path.$download);
Feb 10 '11 #2
empiresolutions
162 100+
the files are hosted on a CDN.
Feb 10 '11 #3
code green
1,726 Expert 1GB
You are not checking fread for error, so if the last chunk fails,
you will not see it.
Feb 10 '11 #4
empiresolutions
162 100+
can you give me an example of how to read the error?
Feb 10 '11 #5
code green
1,726 Expert 1GB
fread returns a string or FALSE on error.
This doesn't tell you the problem of course.
But with error_reporting enabled you may be given a clue
Feb 10 '11 #6
empiresolutions
162 100+
$buffer = fread($handle, $chunksize);
echo $buffer;
Does this not count as checking for an error? It returns nothing important that I see.
Feb 10 '11 #7
code green
1,726 Expert 1GB
This does not handle anything when FALSE is returned.
Is error_reporting enabled in php.ini?
Feb 10 '11 #8
empiresolutions
162 100+
error_reporting = E_ERROR;
Feb 10 '11 #9
code green
1,726 Expert 1GB
This settings shows only the basic errors.
When debugging use
Expand|Select|Wrap|Line Numbers
  1. error_reporting(E_ALL | E_STRICT);
At the beginning of scripts.
File errors will then be output
Feb 10 '11 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Anil Gupte | last post by:
Hi! this may be a trivial question, and may not even involve asp, but let me try.... I want people to click on a .wmv file on a website and download it. Currently, when they do that it simply...
3
by: Anil Gupte | last post by:
My original question had to do with forcing a download instead of play for a known mime type. I got some useful pointers, but, I failed. Can someone tell me what is going wrong in this code? ...
17
by: Kevin Goodsell | last post by:
Is there a place I can download the last public draft for C89? What about a draft for C95? What I really need is a list of all standard library functions, macros, types, etc. for a "keyword...
29
by: lec | last post by:
Hi, I'm observing the following: If I commit the following records 1,2,3,4,5,6,7,8,9,10 to the database and the server hangs, I could lose records 5,6,7,8,9 but record 10 is there. How is this...
6
by: SQLcat | last post by:
I have a VBScript as follows: Dim xmlHTTP : Set xmlHTTP = CreateObject("Microsoft.XMLHTTP") Dim adoStream : Set adoStream = CreateObject("adodb.stream") Const bGetAsAsync = False ' wait for...
3
by: Bouffa | last post by:
Hello everyone, I suppose you all know force-download scripts. The problem is that these scripts don't allow files to be splitted when downloading them via a download manager. I've found a...
7
by: DemonWasp | last post by:
I've been having some trouble getting the Scanner class to operate the way I'd like. I'm doing some fairly basic file IO and I can't seem to get the class to load the last line/token any way I try....
6
by: empiresolutions | last post by:
Using the following code, files greater than 10Megs do not download. Less than 10 is working. Can anyone explain why? Files that will be downloaded are 5G plus in size. function download() {...
0
by: dotnet2005 | last post by:
Hi Toall , I want to explain you clearly , I am having some panels having some controls CheckBox,ComboBox,Datagridview,Hyperlink,label,Listbox,Picturebox,RadioButt­ on ,Textbox)...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.