Каждый топик здесь посвящен конкретной библиотеке или наработкам конкретного проекта
michail1982 » 12 июл 2009, 15:39
Вот написал библиотечку, которая файлы в поток отдаёт поблочно, незнаю, нужна ли она кому, ведь есть download_helper.php
- Код: Выделить всё
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Stream {
var $file_path = FALSE; var $needle_name = FALSE; var $part_size = 512; var $mime = FALSE; var $use_mime_config = FALSE;function MY_Stream($config = array()) {
if(! function_exists('get_file_info' || ! function_exists('get_mime_by_extension'))){
$CI =& get_instance();
$CI->load->helper('file');
}
$this->initialize($config);
log_message('debug', 'Stream Class Initialized');
}
function initialize($config = array())
{
if(sizeof($config > 0)){
foreach ($config as $key => $val) {
if (isset($this->$key))
{
$method = 'set_'.$key;
if (method_exists($this, $method))
{
$this->$method($val);
}
else
{
$this->$key = $val;
}
}
}
}
}
function set_part_size($part_size = FALSE){
$part_size = abs(intval($part_size));
if($part_size>0){
$this->part_size = $part_size;
}
}
function set_mime($mime = FALSE){
$mime = strtolower(trim($mime));
if(preg_match('/^([a-z]+)\/([a-z0-9\+\-\.]+)$/',$mime)){
$this->mime = $mime;
}
}
function send_content($config = array()){
if(sizeof($config)>0){
$this->initialize($config);
}
if(!$this->file_path){
log_message('error', 'Stream Class: Empty filePath');
return FALSE;
}
if(!$file_info = get_file_info($this->file_path,array('name','server_path','size','date','readable'))){
log_message('error', 'Stream Class: File not exists');
return FALSE;
} else {
$fileinfo['date'] = date('D, d M Y H:i:s T', $fileinfo['date']);
}
if(!$file_info['readable']){
log_message('error', 'Stream Class: file not readable');
return FALSE;
}
if(!$needle_name = $this->needle_name){
$needle_name = $file_info['name'];
}
$range = 0;
$handle = @fopen($file_info['server_path'], FOPEN_READ);
if (!$handle){
log_message('error', 'Stream Class: Cannot read file :-/');
return FALSE;
}
if (@$_SERVER['HTTP_RANGE']) {
$range = @$_SERVER['HTTP_RANGE'];
$range = str_replace('bytes=', '', $range);
$range = str_replace('-', '', $range);
if ($range) {
fseek($handle, $range);
}
}
if(!$content_type = $this->mime){
if($this->use_mime_config && $content_type = get_mime_by_extension($needle_name)){
continue;
} else {
if(isset($_SERVER['HTTP_USER_AGENT']) and strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')){
$content_type = 'application/force-download';
}
else{
$content_type = 'application/octet-stream';
}
}
}
if ($range) {
$server_responce = '206 Partial Content';
} else {
$server_responce = 'HTTP/1.1 200 OK';
}
header('HTTP/1.1 '.$server_responce);
header('Content-Disposition: attachment; filename="'.$needle_name.'"');
header('Last-Modified: '.$file_info['date']);
header('Content-Length: '.($file_info['size']-$range));
header('Accept-Ranges: bytes');
header('Content-Range: bytes '.$range.'-'.($file_info['size'] - 1).'/'.$file_info['size']);
header('Content-Type: '.$content_type);
while(!feof($handle)) {
$buf = fread($handle, $this->part_size);
print($buf);
}
fclose($handle);
return TRUE;
}
}
-

michail1982
-
- Сообщения: 533
- Зарегистрирован: 25 ноя 2008, 15:36
-
Вернуться в Решения, авторский код и библиотеки
Кто сейчас на конференции
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 0