note 56946 added to function.fpassthru

From: Date: Tue, 20 Sep 2005 04:12:43 +0000
Subject: note 56946 added to function.fpassthru
Groups: php.notes 
Request: Send a blank email to php-notes+get-95445@lists.php.net to get a copy of this message
I've gathered some useful info regarding sessions and large file downloads. I do not take credit for the function, just putting it in one spot for people. The function: <? function send_file($name) { ob_end_clean(); $path = "/directory/path/".$name; if (!is_file($path) or connection_status()!=0) return(FALSE); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT"); header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT"); header("Content-Type: application/octet-stream"); header("Content-Length: ".(string)(filesize($path))); header("Content-Disposition: inline; filename=$name"); header("Content-Transfer-Encoding: binary\n"); if ($file = fopen($path, 'rb')) { while(!feof($file) and (connection_status()==0)) { print(fread($file, 1024*8)); flush(); } fclose($file); } return((connection_status()==0) and !connection_aborted()); } ?> And now how to use it... <?php session_start(); //Validate the user if($_SESSION[login] == 1){ //Close the session to allow for header() to be sent session_write_close(); if(!send_file("testfile.zip")) { die ("file transfer failed"); } else { //Log the download } }else{ echo "Not logged in"; } ?> This lets a user download a file while still being able to browse your site. ---- Manual Page -- http://www.php.net/manual/en/function.fpassthru.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56946 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56946&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56946&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56946&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56946&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56946&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+56946&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56946&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56946&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#95445) next »