473,385 Members | 1,445 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,385 software developers and data experts.

Download fails on files greater than 10M

162 100+
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.

Expand|Select|Wrap|Line Numbers
  1.     function download() {
  2.  
  3.         global $_GET;
  4.  
  5.         //Gather relevent info about file
  6.         $file = "/user/dac420/tts/incoming/".$_GET['name'];
  7.         $len = filesize($file);
  8.         $filename = basename($file);
  9.         $file_extension = strtolower(substr(strrchr($filename,"."),1));
  10.  
  11.         // Determine correct MIME type
  12.         switch($file_extension){
  13.  
  14.             case "asf":     $ctype = "video/x-ms-asf";                break;
  15.             case "avi":     $ctype = "video/x-msvideo";               break;
  16.             case "exe":     $ctype = "application/octet-stream";      break;
  17.             case "mov":     $ctype = "video/quicktime";               break;
  18.             case "mp3":     $ctype = "audio/mpeg";                    break;
  19.             case "mpg":     $ctype = "video/mpeg";                    break;
  20.             case "mpeg":    $ctype = "video/mpeg";                    break;
  21.             case "rar":     $ctype = "encoding/x-compress";           break;
  22.             case "txt":     $ctype = "text/plain";                    break;
  23.             case "wav":     $ctype = "audio/wav";                     break;
  24.             case "wma":     $ctype = "audio/x-ms-wma";                break;
  25.             case "wmv":     $ctype = "video/x-ms-wmv";                break;
  26.             case "zip":     $ctype = "application/x-zip-compressed";  break;
  27.             default:        $ctype = "application/force-download";    break;
  28.  
  29.         }
  30.  
  31.         //Begin writing headers
  32.         header("Cache-Control:");
  33.         header("Cache-Control: public");
  34.  
  35.         //Use the switch-generated Content-Type
  36.         header("Content-Type: $ctype");
  37.  
  38.         if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
  39.             # workaround for IE filename bug with multiple periods / multiple dots in filename
  40.             # that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe
  41.             $iefilename = preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1);
  42.             header("Content-Disposition: attachment; filename=\"$iefilename\"");
  43.         } else {
  44.             header("Content-Disposition: attachment; filename=\"$filename\"");
  45.         }
  46.  
  47.         header("Accept-Ranges: bytes");
  48.  
  49.         $size=filesize($file);
  50.  
  51.         //check if http_range is sent by browser (or download manager)
  52.         if(isset($_SERVER['HTTP_RANGE'])) {
  53.  
  54.             list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']);
  55.  
  56.             //if yes, download missing part
  57.             str_replace($range, "-", $range);
  58.             $size2=$size-1;
  59.             $new_length=$size2-$range;
  60.  
  61.             header("HTTP/1.1 206 Partial Content");
  62.             header("Content-Length: $new_length");
  63.             header("Content-Range: bytes $range$size2/$size");
  64.  
  65.         } else {
  66.  
  67.             $size2=$size-1;
  68.             header("Content-Range: bytes 0-$size2/$size");
  69.             header("Content-Length: ".$size);
  70.  
  71.         }
  72.  
  73.         //open the file
  74.         $fp=fopen("$file","rb");
  75.  
  76.         //seek to start of missing part
  77.         fseek($fp,$range);
  78.  
  79.         //start buffered download
  80.         while(!feof($fp)){
  81.  
  82.             //reset time limit for big files
  83.             set_time_limit(0);
  84.  
  85.             print(fread($fp,filesize($file)));
  86.             flush();
  87.             ob_flush();
  88.  
  89.         }
  90.  
  91.         fclose($fp);
  92.         exit;
  93.  
  94.     }
  95.  
Mar 4 '08 #1
6 1661
MarkoKlacar
296 Expert 100+
Hi,

Did you set any limit to the file size in the form you use to upload the files?
Mar 5 '08 #2
empiresolutions
162 100+
The solution is to split the file into chunks when sending. Thanks to everyone for your suggestions.

replace the while() in my above code.

Expand|Select|Wrap|Line Numbers
  1.     //start buffered download
  2.     while(!feof($fp)){
  3.  
  4.         $total     = filesize($file);
  5.         $sent      = 0;
  6.         $blocksize = (2 << 20); //2M chunks
  7.         $handle    = fopen($file, "r");
  8.  
  9.         // Now we need to loop through the file and echo out chunks of file data
  10.         // Dumping the whole file fails at > 30M!
  11.         while($sent < $total){
  12.             echo fread($handle, $blocksize);
  13.             $sent += $blocksize;
  14.         }
  15.  
  16.         exit(0);
  17.         @flush();
  18.         @ob_flush();
  19.     }
  20.  
Mar 10 '08 #3
empiresolutions
162 100+
So now that I have the download working I need to know why the page is not refreshing at the end of the script. The download happen, then nothing. How do i get the page to continue after download?
Mar 10 '08 #4
MarkoKlacar
296 Expert 100+
So now that I have the download working I need to know why the page is not refreshing at the end of the script. The download happen, then nothing. How do i get the page to continue after download?
Hi,

What you can do is send the user to the correct page using header.

Good luck
Mar 10 '08 #5
Markus
6,050 Expert 4TB
Nothing greatly important here but, you don't need to globalise GET as it is already a SUPERGLOBAL.

Is that correct?
Mar 10 '08 #6
empiresolutions
162 100+
Hi,

What you can do is send the user to the correct page using header.

Good luck
header() is not working after the feof() call
Mar 11 '08 #7

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

Similar topics

6
by: Jatin | last post by:
Hey Guys I have a web application that allows users to download files. But the files are not hosted on the webserver. The files are stored on external servers and are referenced by a URL/URI....
5
by: gjzusenet | last post by:
Hello. Though Python supports threading, I think it is limited to python code - as soon as you issue a command that uses an external (C?) module, all of your python threads hang until this command...
7
by: Richie Kernagan | last post by:
Howdy Problem with the fread() call on MS Visual Studio 2005 running under Win 64. The parameters to fread size and count are both size_t ie 64 bits. Even so the call fails when the size arg...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.