• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ci 2.0 - large file downloads

#1
[eluser]MVUG[/eluser]
Hi guys,

has anybody an updated downloadhelper that supports large file downloads?

http://nl3.php.net/manual/en/function.fgets.php#59393

#2
[eluser]InsiteFX[/eluser]
You need to modify your php.ini file for larger file downloads!

InsiteFX

#3
[eluser]MVUG[/eluser]
[quote author="InsiteFX" date="1297965038"]You need to modify your php.ini file for larger file downloads!

InsiteFX[/quote]

Ok but wouldn't it be nice that there is a function that doesn't need to change ini settings?

#4
[eluser]Twisted1919[/eluser]
Also it would be nice if the upload limit in php.ini would be set to 2 MB and you could upload 10 MB files, right ?

#5
[eluser]MVUG[/eluser]
[quote author="Twisted1919" date="1297966013"]Also it would be nice if the upload limit in php.ini would be set to 2 MB and you could upload 10 MB files, right ?[/quote]

Uuhm? What do you mean? But nevermind, I'll figure it out on my own!

#6
[eluser]MVUG[/eluser]
I added a helper, http://codeigniter.com/wiki/Download_hel...rge_files/

#7
[eluser]Twisted1919[/eluser]
you can force download of any file with this code, doesn't matter it's size:
Code:
$file_name='download-me.zip';
        $file_path=FCPATH.'downloads/'.$file_name;

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=download-item-'.$file_name);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file_path));
        ob_clean();
        flush();
        readfile($file_path);
        exit;


Your helper, assumes that $filename is a full path to the file, which is okay, but you also set this name when the file is downloaded, as far as i can see, so i don't agree this. (downloading something like /var/www/public_html/downloads/download-me.zip as filename can only mean a security issue)

#8
[eluser]MVUG[/eluser]
[quote author="Twisted1919" date="1297968822"]you can force download of any file with this code, doesn't matter it's size:
Code:
$file_name='download-me.zip';
        $file_path=FCPATH.'downloads/'.$file_name;

        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=download-item-'.$file_name);
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file_path));
        ob_clean();
        flush();
        readfile($file_path);
        exit;


Your helper, assumes that $filename is a full path to the file, which is okay, but you also set this name when the file is downloaded, as far as i can see, so i don't agree this. (downloading something like /var/www/public_html/downloads/download-me.zip as filename can only mean a security issue)[/quote]

Yeah your right, I try to fix this tonight... Also test your code! Thanx btw!

#9
[eluser]PoetaWD[/eluser]
Hello,

This is a BYTESERVING script. This will allow you to serve BIG FILES using PHP and also allow your user to use a DOWNLOAD MANAGER to download the file.

Just add those functions on your controller and use:

Code:
//unset magic quotes; otherwise, file contents will be modified
        set_magic_quotes_runtime(0);
        
        //do not send cache limiter header
        ini_set('session.cache_limiter','none');
        
        
        $filename= YOUR FILE PATH HERE; //this is the path to your code !
        $this->byteserve($filename); //byteserve it!


Code:
function set_range($range, $filesize, &$first, &$last){

      $dash=strpos($range,'-');
      $first=trim(substr($range,0,$dash));
      $last=trim(substr($range,$dash+1));
      if ($first=='') {
        //suffix byte range: gets last n bytes
        $suffix=$last;
        $last=$filesize-1;
        $first=$filesize-$suffix;
        if($first<0) $first=0;
      } else {
        if ($last=='' || $last>$filesize-1) $last=$filesize-1;
      }
      if($first>$last){
        //unsatisfiable range
        header("Status: 416 Requested range not satisfiable");
        header("Content-Range: */$filesize");
        exit;
      }
    }
    
    function buffered_read($file, $bytes, $buffer_size=1024){
      /*
      Outputs up to $bytes from the file $file to standard output, $buffer_size bytes at a time.
      */
      $bytes_left=$bytes;
      while($bytes_left>0 && !feof($file)){
        if($bytes_left>$buffer_size)
          $bytes_to_read=$buffer_size;
        else
          $bytes_to_read=$bytes_left;
        $bytes_left-=$bytes_to_read;
        $contents=fread($file, $bytes_to_read);
        echo $contents;
        flush();
      }
    }
    
    function byteserve($filename){

    
      $filesize=filesize($filename);
      $file=fopen($filename,"rb");
    
      $ranges=NULL;
      if ($_SERVER['REQUEST_METHOD']=='GET' && isset($_SERVER['HTTP_RANGE']) && $range=stristr(trim($_SERVER['HTTP_RANGE']),'bytes=')){
        $range=substr($range,6);
        $boundary='g45d64df96bmdf4sdgh45hf5';//set a random boundary
        $ranges=explode(',',$range);
      }
    
      if($ranges && count($ranges)){
        header("HTTP/1.1 206 Partial content");
        header("Accept-Ranges: bytes");
        if(count($ranges)>1){
          /*
          More than one range is requested.
          */
    
          //compute content length
          $content_length=0;
          foreach ($ranges as $range){
            $this->set_range($range, $filesize, $first, $last);
            $content_length+=strlen("\r\n--$boundary\r\n");
            $content_length+=strlen("Content-type: application/zip\r\n");
            $content_length+=strlen("Content-range: bytes $first-$last/$filesize\r\n\r\n");
            $content_length+=$last-$first+1;          
          }
          $content_length+=strlen("\r\n--$boundary--\r\n");
    
          //output headers
          header("Content-Length: $content_length");
          //see http://httpd.apache.org/docs/misc/known_client_problems.html for an discussion of x-byteranges vs. byteranges
          header("Content-Type: multipart/x-byteranges; boundary=$boundary");
    
          //output the content
          foreach ($ranges as $range){
            $this->set_range($range, $filesize, $first, $last);
            echo "\r\n--$boundary\r\n";
            echo "Content-type: application/zip\r\n";
            echo "Content-range: bytes $first-$last/$filesize\r\n\r\n";
            fseek($file,$first);
            $this->buffered_read ($file, $last-$first+1);          
          }
          echo "\r\n--$boundary--\r\n";
        } else {
          /*
          A single range is requested.
          */
          $range=$ranges[0];
          $this->set_range($range, $filesize, $first, $last);  
          header("Content-Length: ".($last-$first+1) );
          header("Content-Range: bytes $first-$last/$filesize");
          header("Content-Type: application/zip");  
          fseek($file,$first);
          $this->buffered_read($file, $last-$first+1);
        }
      } else{
        //no byteserving
        header("Accept-Ranges: bytes");
        header("Content-Length: $filesize");
        header("Content-Type: application/zip");
        readfile($filename);
      }
      fclose($file);
    }
    
    function serve($filename, $download=0){
      //Just serves the file without byteserving
      //if $download=true, then the save file dialog appears
      $filesize=filesize($filename);
      header("Content-Length: $filesize");
      header("Content-Type: application/zip");
      $filename_parts=pathinfo($filename);
      if($download) header('Content-disposition: attachment; filename='.$filename_parts['basename']);
      readfile($filename);
    }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.