From e2edb4dd56c1f7afdcf313c972c81d958b2b6d52 Mon Sep 17 00:00:00 2001 From: rylon Date: Mon, 16 Jun 2014 14:55:14 +0200 Subject: [PATCH] files header fix for downloads --- files/files.php | 22 ++++++++++++++++++++-- system/HEADER.php | 5 ++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/files/files.php b/files/files.php index 1690800..2e74fa8 100644 --- a/files/files.php +++ b/files/files.php @@ -24,8 +24,13 @@ class files { $ext = pathinfo(self::$folders[$cat].$id); $ext = strtoupper(array_key_exists('extension', $ext) ? $ext['extension'] : ''); if(\SYSTEM\HEADER::available($ext)){ - call_user_func('\SYSTEM\HEADER::'.$ext);} - return file_get_contents(self::$folders[$cat].$id); + call_user_func('\SYSTEM\HEADER::'.$ext); + }else{ + \SYSTEM\HEADER::FILE($id);} + + if(!self::file_get_contents_chunked(self::$folders[$cat].$id,4096,function($chunk,&$handle,$iteration){echo $chunk;})){ + throw new \SYSTEM\LOG\ERROR("Could not transfere File.");} + return; } public static function put($cat, $id, $contents) { @@ -60,4 +65,17 @@ class files { public static function getURL($cat, $id = null) { return \SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL) . 'api.php?call=files&cat=' . $cat . '&id=' . $id;} + + private static function file_get_contents_chunked($file,$chunk_size,$callback) + { + $handle = fopen($file, "r"); + $i = 0; + while (!feof($handle)) + { + call_user_func_array($callback,array(fread($handle,$chunk_size),&$handle,$i)); + $i++; + } + fclose($handle); + return true; + } } \ No newline at end of file diff --git a/system/HEADER.php b/system/HEADER.php index 7e13fc7..b21c85f 100644 --- a/system/HEADER.php +++ b/system/HEADER.php @@ -2,7 +2,6 @@ namespace SYSTEM; class HEADER { - private static function checkHeader(){ $file = null; $line = null; @@ -28,6 +27,10 @@ class HEADER { public static function GIF(){ if(self::checkHeader()){ header('content-type:image/gif;');}} + + public static function FILE($filename){ + header("Content-type: application/octet-stream"); + header("Content-Disposition: attachment; filename=\"".$filename."\"");} public static function available($datatype){ $datatype = strtoupper($datatype);