note 94741 added to function.ob-start

From: Date: Sat, 21 Nov 2009 14:58:36 +0000
Subject: note 94741 added to function.ob-start
Groups: php.notes 
Request: Send a blank email to php-notes+get-163168@lists.php.net to get a copy of this message
Hey folks, as dan already noted, using the ob_gzhandler will make it impossible to get the content-length, and thusly will make the download of files a little ugly. the client cant show progress as it wont know how large it's targeted download actually is. even if you write a custom handler, it seems impossible to set the buffer's size to a global. the ob_handler seems to not share a global scope. i wrote a small php to solve that problem, by not using the ob_* functions, but rather gzencode manually: <?php $file = $_REQUEST["file"] or die("Please specify the 'file' parameter."); $allowedExtensions = array( "jpg" => "image", "png" => "image", "gif" => "image" ); $path = pathinfo($file); if(!in_array(strtolower($path["extension"]), array_keys($allowedExtensions))) { header("HTTP/1.0 403 Access denied"); die("403<br/>You dont have access to that file, wether it exists or not."); } $pack = true; if(!empty($_SERVER["HTTP_ACCEPT_ENCODING"]) && strpos("gzip",$_SERVER["HTTP_ACCEPT_ENCODING"]) === NULL) $pack = false; if(!is_file($file) || !is_readable($file)) { header("HTTP/1.0 404 Not Found"); die("404<br/>The file you are looking for is not available."); } $buffer = file_get_contents($file); if($pack) { header("Content-Encoding: gzip"); $buffer = gzencode($buffer,9,true); } $length = strlen($buffer); header("Content-Type: {$allowedExtensions[$path["extension"]]}/{$path["extension"]}"); header("Content-Length: ".$length); die($buffer); ?> this will proxy images to a client, gzipping them at the highest compression-level. to save the compression's overhead one could cache compressed outputs. ---- Server IP: 212.124.37.9 Probable Submitter: 85.182.49.194 ---- Manual Page -- http://www.php.net/manual/en/function.ob-start.php Edit -- https://master.php.net/note/edit/94741 Del: integrated -- https://master.php.net/note/delete/94741/integrated Del: useless -- https://master.php.net/note/delete/94741/useless Del: bad code -- https://master.php.net/note/delete/94741/bad+code Del: spam -- https://master.php.net/note/delete/94741/spam Del: non-english -- https://master.php.net/note/delete/94741/non-english Del: in docs -- https://master.php.net/note/delete/94741/in+docs Del: other reasons-- https://master.php.net/note/delete/94741 Reject -- https://master.php.net/note/reject/94741 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163168) next »