$exts ) { if ( in_array( $ext, $exts ) ) { $mime = $type; break; } } if ( preg_match( '#^text/#', $mime ) ) { // Text should have a charset=UTF-8 (PHP's webserver does this too) header( "Content-Type: $mime; charset=UTF-8" ); } else { header( "Content-Type: $mime" ); } $content = file_get_contents( $file ); header( 'Vary: Accept-Encoding' ); $acceptGzip = preg_match( '/\bgzip\b/', $_SERVER['HTTP_ACCEPT_ENCODING'] ?? '' ); if ( $acceptGzip && // Don't compress binary static files (e.g. png) preg_match( '/text|javascript|json|css|xml|svg/', $mime ) && // Tiny files tend to grow instead of shrink. – strlen( $content ) > 150 ) { $content = gzencode( $content, 9 ); header( 'Content-Encoding: gzip' ); } header( "Content-Length: " . strlen( $content ) ); echo $content; return true; }