note 58042 added to function.fpassthru

From: Date: Sat, 22 Oct 2005 03:36:19 +0000
Subject: note 58042 added to function.fpassthru
Groups: php.notes 
Request: Send a blank email to php-notes+get-97400@lists.php.net to get a copy of this message
here is my code, i tried several combinations, but most of them didnt work, and had all kinds of unnecessary headers in them, etc. this has additional good features, such as it stops sending the file if the connection stops (hopefully it does anyways), and it fixes IE filename problems when sending files that contain more than one dot in them by using a simple preg_replace (IE likes to terminate the filename and messes everything up): <? function send_file($path) { session_write_close(); ob_end_clean(); if (!is_file($path) || connection_status()!=0) return(FALSE); //to prevent long file from getting cut off from //max_execution_time set_time_limit(0); $name=basename($path); //filenames in IE containing dots will screw up the //filename unless we add this if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) $name = preg_replace('/\./', '%2e', $name, substr_count($name, '.') - 1); //required, or it might try to send the serving //document instead of the file header("Cache-Control: "); header("Pragma: "); header("Content-Type: application/octet-stream"); header("Content-Length: " .(string)(filesize($path)) ); header('Content-Disposition: attachment; filename="'.$name.'"'); header("Content-Transfer-Encoding: binary\n"); if($file = fopen($path, 'rb')){ while( (!feof($file)) && (connection_status()==0) ){ print(fread($file, 1024*8)); flush(); } fclose($file); } return((connection_status()==0) and !connection_aborted()); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.fpassthru.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58042 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58042&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58042&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58042&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58042&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58042&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58042&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58042&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58042&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97400) next »