Forcing downloads with PHP

The DownloadFile() function will initiate a download from the path that you provided. It works great with large files because it buffers the data, instead of putting the entire content of the file in memory and serving it from there.
1.  <?php
2.  function DownloadFile($File)
3.  {
4.      if (!is_file($File))
5.          {
6.                  die("404 File not found!");
7.          }
8.     
9.      $FileName = basename($File);
10.     $FileExt = strtolower(substr(strrchr($FileName,"."),1));
11.    
12.     switch($FileExt)
13.         {
14.         case "exe":
15.                         $ctype = "application/octet-stream";
16.                         break;
17.         case "zip":
18.                         $ctype = "application/zip";
19.                         break;
20.         case "mp3":
21.                         $ctype = "audio/mpeg";
22.                         break;
23.         case "mpg":
24.                         $ctype = "video/mpeg";
25.                         break;
26.         case "avi":
27.                         $ctype = "video/x-msvideo";
28.                         break;
29.                 case "wmv":
30.                         $ctype = "video/x-ms-wmv";
31.                         break;
32.         default:
33.                         $ctype="application/force-download";
34.     }
35.    
36.     header("Cache-Control:");
37.     header("Cache-Control: public");
38.    
39.     header("Content-Type: $ctype");
40.     if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
41.         {
42.         // IE Bug workaround
43.         $IEFileName = preg_replace('/\./', '%2e', $FileName, substr_count($FileName, '.') - 1);
44.         header("Content-Disposition: attachment; filename=\"$IEFileName\"");
45.     }
46.         else
47.         {
48.         header("Content-Disposition: attachment; filename=\"$FileName\"");
49.     }
50.     header("Accept-Ranges: bytes");
51.    
52.     $FileSize = filesize($File);
53.     // If http_range is sent by browser (or download manager)
54.     if(isset($_SERVER['HTTP_RANGE']))
55.         {
56.         list($a, $Range) = explode("=",$_SERVER['HTTP_RANGE']);
57.         // If yes, download missing part
58.         str_replace($Range, "-", $Range);
59.         $FileSize2 = $FileSize-1;
60.         $new_length = $FileSize2-$Range;
61.         header("HTTP/1.1 206 Partial Content");
62.         header("Content-Length: $new_length");
63.         header("Content-Range: bytes $Range$FileSize2/$FileSize");
64.     }
65.         else
66.         {
67.         $FileSize2 = $FileSize-1;
68.         header("Content-Range: bytes 0-$FileSize2/$FileSize");
69.         header("Content-Length: ".$FileSize);
70.     }
71.     // Open the file
72.     $FP = fopen("$File","rb");
73.     // Seek to start of missing part
74.     fseek($FP,$Range);
75.     // Start buffered download
76.     while(!feof($FP))
77.         {
78.         // Reset time limit so there's no timeout
79.         set_time_limit(0);
80.         print(fread($FP,1024*8));
81.         flush();
82.         ob_flush();
83.     }
84.     fclose($FP);
85.     exit;
86. }
87. // How to use it
88. DownloadFile("F:\Geekpedia Exposed.wmv");
89. ?>
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top