Partage
  • Partager sur Facebook
  • Partager sur Twitter

Download de fichiers

Content-Disposition: inline

    9 août 2012 à 10:43:52

    Bonjour à tous,

    Je cherche à rendre un script de download générique, j'entends par-là, qu'il puisse servir au download inline & attachment.

    <?php
    	header('Content-Description: File Transfer');
    	header('Content-Type: '$download_mime);
    	header('Content-Transfer-Encoding: binary');
    	header('Content-Range: '.$download_range);
    	header('Content-Length: '.$download_size);      
    	$download_handle=fopen($download_path,"rb");
    	fseek($download_handle,$download_bytes);
    	while(!feof($download_handle)){
    		print(fread($download_handle,8192));
    		flush();
    		usleep($download_delay);
    	}
    	fclose($download_handle);
    


    Est-ce invalide, pour du inline (CSS, JS, images, ...), svp?
    • Partager sur Facebook
    • Partager sur Twitter
    Anonyme
      9 août 2012 à 10:53:36

      Me semble qu'il te faut encore spécifier le Content-Type et le Content-Encoding.

      Aussi, je suppose que pour de l'inline de text genre CSS ou JS, Content-Transfer-Encoding: **binary** ne passera pas.
      • Partager sur Facebook
      • Partager sur Twitter
        9 août 2012 à 12:38:58

        Ok, merci...

        J'attends tout de même de voir s'il y a d'autres avis. :)
        • Partager sur Facebook
        • Partager sur Twitter
          10 août 2012 à 13:02:19

          Bonjour,

          Voici ma classe actuelle (100% fonctionnelle) :

          <?php
          	class Downloader_Tools{
          		private $_maxSpeed;
          		private $_maxLength;
          		private $_delay;
          		private $_resumeable;
          		private $_mode;
          		private $_path;
          		private $_basename;
          		private $_contentType;
          		private $_size;
          		private $_contentDisposition;
          		private $_startRange;
          		private $_endRange;
          		public function __construct(){
          			@session_start();
          			session_write_close();
          			set_time_limit(0);
          			if(ini_get('zlib.output_compression')){
          				ini_set('zlib.output_compression','Off');
          			}
          			$this->_maxSpeed=abs((int)DOWNLOAD_TOOLS_SPEED);
          			$this->_maxLength=abs((int)DOWNLOAD_TOOLS_LENGTH);
          			$this->_delay=round(1000000*$this->_maxLength/$this->_maxSpeed);
          			$this->_resumeable=((bool)DOWNLOAD_TOOLS_RESUMEABLE)&&(isset($_SERVER['HTTP_RANGE'])||isset($_ENV['HTTP_RANGE']));
          		}
          		public function download($path){
          			$this->_mode=__METHOD__;
          			$this->_contentDisposition='inline';
          			$this->_read($path);
          		}
          		public function forceDownload($path){
          			$this->_mode=__METHOD__;
          			$this->_contentDisposition='attachment';
          			$this->_read($path);
          		}
          		private function _read($path){
          			$this->_readFileInfo($path);
          			$this->_setRange();
          			$this->_sendHeaders();
          			$handle=fopen(
          				$path,
          				$this->_contentType=='application/force-download'||$this->_contentType=='application/octet-stream'?
          					'rb':'r'
          			);
          			fseek($handle,$this->_startRange);
          			$remainingSize=$this->_endRange-$this->_startRange+1;
          			$length=$remainingSize<$this->_maxLength?$remainingSize:$this->_maxLength;
          			while(false!==($data=fread($handle,$length))&&$remainingSize>0){
          				echo $data;
          				$remainingSize-=$length;
          				if($remainingSize<$length){
          					$length=$remainingSize;
          				}
          				flush();
          				usleep($this->_delay);
          			}
          			fclose($handle);
          			$this->_exit();
          		}
          		private function _readFileInfo($path){
          			if(!is_readable($path)){
          				$this->_exit('HTTP/1.0 404 Not Found');
          			}
          			$this->_path=$path;
          			$this->_basename=basename($path);
          			$this->_size=filesize($path);
          			$finfo=new finfo(FILEINFO_MIME_TYPE);
          			$finfoFile=$finfo->file($path);
          			$this->_contentType=$this->_mode=='forceDownload'?
          				'application/force-download':strpos($finfoFile,'charset=binary')?
          					'application/octet-stream':$finfoFile;
          		}
          		private function _setRange(){
          			$this->_startRange=0;
          			$this->_endRange=$this->_size-1;
          			if($this->_mode=='forceDownload'&&$this->_resumeable){
          				$httpRange='';
          				$results=[];
          				if(isset($_SERVER['HTTP_RANGE'])){
          					$httpRange=$_SERVER['HTTP_RANGE'];
          				}
          				elseif(isset($_ENV['HTTP_RANGE'])){
          					$httpRange=$_SERVER['HTTP_RANGE'];
          				}
          				if(!preg_match('#bytes=([0-9]+)?-([0-9]+)?(/[0-9]+)?#i',$httpRange,$results)){
          					$this->_exit('HTTP/1.1 416 Requested Range Not Satisfiable');
          				}
          				$this->_startRange=!empty($results[1])?(int)$results[1]:null;
          				$this->_endRange=!empty($results[2])?(int)$results[2]:$this->_endRange;
          				if($this->_startRange===null){
          					$this->_startRange=$this->_size-$this->_endRange;
          					$this->_endRange-=1;
          				}
          			}
          		}
          		private function _sendHeaders(){
          			header('Content-Disposition: '.$this->_contentDisposition.'; filename="'.$this->_basename.'"');
          			if($this->_mode=='forceDownload'){
          				header('Accept-Ranges: bytes');
          				header('Cache-Control: no-cache, must-revalidate');
          				header('Cache-Control: post-check=0,pre-check=0');
          				header('Cache-Control: max-age=0');
          				header('Content-Description: File Transfer');
          				header('Content-Transfer-Encoding: binary');
          				header('Expires: 0');
          				header('Pragma: no-cache');
          				if($this->_resumeable){
          					header('HTTP/1.1 206 Partial Content');
          					header('Content-Range: '.$this->_startRange.'-'.$this->_endRange.'/'.$this->_size);
          				}
          			}
          			header('Content-Type: '.$this->_contentType);
          			header('Content-Length: '.($this->_endRange-$this->_startRange+1));
          		}
          		private function _exit($header=null){
          			if($header!==null){
          				header($header);
          			}
          			exit;
          		}
          	}
          ?>
          


          Qu'en pensez-vous? Est-ce assez optimisé? Y manque-t-il encore des choses? Est-ce que, de votre point de vue, il y a des erreurs?
          • Partager sur Facebook
          • Partager sur Twitter

          Download de fichiers

          × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
          × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
          • Editeur
          • Markdown