note 65667 added to function.header

From: Date: Fri, 05 May 2006 13:00:45 +0000
Subject: note 65667 added to function.header
Groups: php.notes 
Request: Send a blank email to php-notes+get-110624@lists.php.net to get a copy of this message
I've created script that gives users ability to download files from closed directory. It supports multithread download and download resuming <?php $fname = $_GET['file']; $fpath = "downloads/$fname"; $fsize = filesize($fpath); $bufsize = 20000; if(isset($_SERVER['HTTP_RANGE'])) //Partial download { if(preg_match("/^bytes=(\\d+)-(\\d*)$/", $_SERVER['HTTP_RANGE'], $matches)) { //parsing Range header $from = $matches[1]; $to = $matches[2]; if(empty($to)) { $to = $fsize - 1; // -1 because end byte is included //(From HTTP protocol: // 'The last-byte-pos value gives the byte-offset of the last byte in the range; that is, the byte positions specified are inclusive') } $content_size = $to - $from + 1; header("HTTP/1.1 206 Partial Content"); header("Content-Range: $from-$to/$fsize"); header("Content-Length: $content_size"); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=$fname"); header("Content-Transfer-Encoding: binary"); if(file_exists($fpath) && $fh = fopen($fpath, "rb")) { fseek($fh, $from); $cur_pos = ftell($fh); while($cur_pos !== FALSE && ftell($fh) + $bufsize < $to+1) { $buffer = fread($fh, $bufsize); print $buffer; $cur_pos = ftell($fh); } $buffer = fread($fh, $to+1 - $cur_pos); print $buffer; fclose($fh); } else { header("HTTP/1.1 404 Not Found"); exit; } } else { header("HTTP/1.1 500 Internal Server Error"); exit; } } else // Usual download { header("HTTP/1.1 200 OK"); header("Content-Length: $fsize"); header("Content-Type: application/force-download"); header("Content-Disposition: attachment; filename=$fname"); header("Content-Transfer-Encoding: binary"); if(file_exists($fpath) && $fh = fopen($fpath, "rb")){ while($buf = fread($fh, $bufsize)) print $buf; fclose($fh); } else { header("HTTP/1.1 404 Not Found"); } } ?> ---- Server IP: 195.114.128.28 Probable Submitter: 193.239.75.9 (proxied: 192.168.1.30) ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.header.php Edit -- http://master.php.net/note/edit/65667 Del: integrated -- http://master.php.net/note/delete/65667/integrated Del: useless -- http://master.php.net/note/delete/65667/useless Del: bad code -- http://master.php.net/note/delete/65667/bad+code Del: spam -- http://master.php.net/note/delete/65667/spam Del: non-english -- http://master.php.net/note/delete/65667/non-english Del: in docs -- http://master.php.net/note/delete/65667/in+docs Del: other reasons-- http://master.php.net/note/delete/65667 Reject -- http://master.php.net/note/reject/65667 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#110624) next »