1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class Net_HTTPDownload
24: {
25: 26: 27: 28:
29: protected $path;
30:
31: 32: 33: 34: 35:
36: protected $name;
37:
38: 39: 40: 41:
42: protected $extension;
43:
44: 45: 46: 47:
48: protected $mime;
49:
50: 51: 52: 53:
54: protected $size;
55:
56: 57: 58: 59:
60: protected $resume;
61:
62: 63: 64: 65:
66: protected $seekStart;
67:
68: 69: 70: 71:
72: protected $seekEnd;
73:
74: public function __construct($path,$resume = false)
75: {
76: if(file_exists($path))
77: {
78: $this->path = $path;
79: $this->extension = strtolower(substr(strrchr($path,'.'),1));
80: $this->getMime();
81: $this->size = filesize($path);
82: $this->seekStart = 0;
83: $this->seekEnd = -1;
84: $this->resume = $resume;
85: $this->name = basename($path);
86: }
87: else
88: {
89: throw new Exception('Fichier innexistant');
90: }
91: }
92:
93: 94: 95:
96: public function download()
97: {
98: header("Cache-Control: cache, must-revalidate");
99: header("Pragma: public");
100: header('Expires: 0');
101: if($this->resume)
102: {
103: $this->getRange();
104: header('HTTP/1.0 206 Partial Content');
105: header('Status: 206 Partial Content');
106: header('Accept-Ranges: bytes');
107: header('Content-Range: bytes '.$this->seekStart.'-'.$this->seekEnd.'/'.$this->size);
108: header('Content-Length:' . ($this->seekEnd - $this->seekStart + 1));
109: }
110: else
111: {
112: header('Content-Length:' . $this->size);
113: }
114:
115: header('Content-Description: File Transfer');
116: header('Content-Type: '.$this->mime);
117: header('Content-Disposition: attachment;filename="'.$this->name.'"');
118: header('Content-Transfer-Encoding: binary');
119:
120: $handle = fopen($this->path,'rb');
121: if(!$handle)
122: {
123: throw new Exception('Erreur pendant le téléchargement');
124: }
125: else
126: {
127: fseek($handle,$this->seekStart);
128: while(!feof($handle))
129: {
130: set_time_limit(0);
131: echo fread($handle, 1024*8);
132: flush();
133: ob_flush();
134: }
135: fclose($handle);
136: }
137: }
138:
139: 140: 141: 142: 143:
144: public function setMimeType($mime)
145: {
146: $this->mime = $mime;
147: }
148:
149: 150: 151: 152:
153: public function setName($name)
154: {
155: $this->name = $name;
156: }
157:
158: 159: 160: 161: 162:
163: private function getRange()
164: {
165:
166: if(isset($_SERVER['HTTP_RANGE']))
167: {
168: list($unit,$ranges) = explode('=',$_SERVER['HTTP_RANGE'],2);
169: if($unit == 'bytes')
170: {
171: list($range,$extraRange) = explode(',',$ranges,2);
172:
173: list($seekStart,$seekEnd) = explode('-',$range,2);
174:
175: if(!empty($seekStart) && $seekStart > 0)
176: $this->seekStart = intval($seekStart);
177:
178: if(!empty($seekEnd))
179: $this->seekEnd = min(abs(intval($seekEnd)), ($this->size) -1);
180: }
181: }
182: }
183:
184: 185: 186:
187: private function getMime()
188: {
189: switch($this->extension)
190: {
191: case 'txt': $this->mime='text/plain'; break;
192: case 'pdf': $this->mime='application/pdf'; break;
193: case 'rtf': $this->mime='application/rtf'; break;
194: case 'jpg': $this->mime='image/jpeg'; break;
195: case 'xls': $this->mime='application/vnd.ms-excel'; break;
196: case 'pps': $this->mime='application/vnd.ms-powerpoint'; break;
197: case 'doc': $this->mime='application/msword'; break;
198: case 'exe': $this->mime='application/octet-stream'; break;
199: case 'zip': $this->mime='application/zip'; break;
200: case 'mp3': $this->mime='audio/mpeg'; break;
201: case 'mpg': $this->mime='video/mpeg'; break;
202: case 'avi': $this->mime='video/x-msvideo'; break;
203: default: $this->mime='application/force-download';
204: }
205: }
206: }