Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

  • Net_HTTPDownload
  • Net_Socket
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Pry Framework
  4:  *
  5:  * LICENSE
  6:  *
  7:  * This source file is subject to the new BSD license that is bundled
  8:  * with this package in the file LICENSE.txt.
  9:  * 
 10:  * @version $Revision: 276 $
 11:  */
 12: 
 13: /**
 14:  * Téléchargement de fichier via protocole HTTP
 15:  * Pour des fichiers de taille importante préférer la solution X-SendFile header
 16:  * @category Pry
 17:  * @package Net
 18:  * @see http://www.php.net/manual/en/function.fread.php#84115
 19:  * @version 1.0.2
 20:  * @author Olivier ROGER <oroger.fr>
 21:  *
 22:  */
 23: class Net_HTTPDownload
 24: {
 25:     /**
 26:      * Chemin vers le fichier
 27:      * @var string
 28:      */
 29:     protected $path;
 30: 
 31:     /**
 32:      * Nom du fichier envoyé au navigateur.
 33:      * Permet de renommer un fichier en l'envoyant.
 34:      * @var string
 35:      */
 36:     protected $name;
 37: 
 38:     /**
 39:      * Extension du fichier
 40:      * @var string
 41:      */
 42:     protected $extension;
 43: 
 44:     /**
 45:      * Type Mime du fichier
 46:      * @var string
 47:      */
 48:     protected $mime;
 49: 
 50:     /**
 51:      * Taille du fichier en octet
 52:      * @var int
 53:      */
 54:     protected $size;
 55: 
 56:     /**
 57:      * Active ou non la reprise de téléchargement
 58:      * @var boolean
 59:      */
 60:     protected $resume;
 61: 
 62:     /**
 63:      * Position de début du pointeur
 64:      * @var int
 65:      */
 66:     protected $seekStart;
 67: 
 68:     /**
 69:      * Position de fin du pointeur
 70:      * @var int
 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:      * Lance le téléchargement ou la reprise du téléchargement
 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); //Paquet de 8ko
132:                 flush();
133:                 ob_flush();
134:             }
135:             fclose($handle);
136:         }
137:     }
138: 
139:     /**
140:      * Défini le type mime du fichier
141:      * Par défaut le type octet-stream est utilisé
142:      * @param string $mime
143:      */
144:     public function setMimeType($mime)
145:     {
146:         $this->mime = $mime;
147:     }
148: 
149:     /**
150:      * Renomme le fichier à télécharger
151:      * @param string $name
152:      */
153:     public function setName($name)
154:     {
155:         $this->name = $name;
156:     }
157: 
158:     /**
159:      * Récupère le range
160:      * @see http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
161:      * @return void
162:      */
163:     private function getRange()
164:     {
165:         //Peut être de la forme Range: bytes=0-99,500-1499,4000-
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:                 //Seul le premier range est utilisé pour des raisons de simplicité
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:      * Tente de déterminer le type mime du fichier
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: }
Pry Framework API documentation generated by ApiGen 2.6.1