note 105471 modified in function.fread by danbrown

From: Date: Sat, 20 Aug 2011 22:15:45 +0000
Subject: note 105471 modified in function.fread by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182613@lists.php.net to get a copy of this message
Concerning [problems with UTF-8 and] downloading Zip files I found that simply adding 3 lines of code before starting the fread to the buffer for delivery in all browsers solved the problem. <?php ob_end_clean(); ob_start(); header( 'Content-Type:' ); ?> ... see where placed in the function below: <?php function readfile_chunked( $filename, $retbytes = true ) { $chunksize = 1 * (1024 * 1024); // how many bytes per chunk $buffer = ''; $cnt = 0; $handle = fopen( $filename, 'rb' ); if ( $handle === false ) { return false; } ob_end_clean(); //added to fix ZIP file corruption ob_start(); //added to fix ZIP file corruption header( 'Content-Type:' ); //added to fix ZIP file corruption while ( !feof( $handle ) ) { $buffer = fread( $handle, $chunksize ); //$buffer = str_replace("","",$buffer); echo $buffer; ob_flush(); flush(); if ( $retbytes ) { $cnt += strlen( $buffer ); } } $status = fclose( $handle ); if ( $retbytes && $status ) { return $cnt; // return num. bytes delivered like readfile() does. } return $status; } ?> --was-- Concerning the UTF-8 problem with downloading Zip files I found that simply adding 3 lines of code before starting the fread to the buffer for delivery in all browsers solved the problem. ob_end_clean(); ob_start(); header( 'Content-Type:' ); ... see where placed in the function below: function readfile_chunked( $filename, $retbytes = true ) { $chunksize = 1 * (1024 * 1024); // how many bytes per chunk $buffer = ''; $cnt = 0; $handle = fopen( $filename, 'rb' ); if ( $handle === false ) { return false; } ob_end_clean(); //added to fix ZIP file corruption ob_start(); //added to fix ZIP file corruption header( 'Content-Type:' ); //added to fix ZIP file corruption while ( !feof( $handle ) ) { $buffer = fread( $handle, $chunksize ); //$buffer = str_replace("","",$buffer); echo $buffer; ob_flush(); flush(); if ( $retbytes ) { $cnt += strlen( $buffer ); } } $status = fclose( $handle ); if ( $retbytes && $status ) { return $cnt; // return num. bytes delivered like readfile() does. } return $status; } http://php.net/manual/en/function.fread.php

« previous php.notes (#182613) next »