Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Output Control Functions : ob_get_length

ob_get_length

Return the length of the output buffer (PHP 4 >= 4.0.2, PHP 5)
int ob_get_length ( )

Example 1688. A simple ob_get_length() example

<?php

ob_start
();

echo
"Hello ";

$len1 = ob_get_length();

echo
"World";

$len2 = ob_get_length();

ob_end_clean();

echo
$len1 . ", ." . $len2;
?>

The above example will output:

6, 11

Code Examples / Notes » ob_get_length

rush

There is a work-around for the situation you need to get length of the gz-ed buffer.
ob_start();
ob_start('ob_gzhandler');
 ... output the page content...
ob_end_flush();  // The ob_gzhandler one
header('Content-Length: '.ob_get_length());
ob_end_flush();  // The main one
more info at http://www.edginet.org/techie/website/http.html


prophp

It's interesting how ob_start("ob_gzhandler"); or ob_gzhandler(); affect this function. The returned size is the buffer uncompressed size; therefore something like strlen(ob_get_contents()); just (hopefully) faster. However in order to obtain the compressed buffer size; there seem to be no function around.
Calin


stangelanda

If you don't buffer your output, it doesn't seem like it would be possible for a web server to output a Content-Length header.  For static files it can check the filesize, but for dynamic files that send output a little by little there is no way to know how many bytes it is going to output.  And the headers have to be sent before a single byte is output.
Also if you're wondering why the Content-Length is important, the browser doesn't close the connection as long as the script is running.  So if you register time consuming shutdown functions, the browser will still 'spin' waiting for more content until they complete.  (I'm not sure this happens under all conditions, but I am certain the sending the Content-Length always prevents this.)
You might take note that this manual page does not currently output any Content-Length header.
Alternatively using mb_strlen($output, 'latin1') seems to work to determine bytes in a string.  strlen may or may not depending on if it has been set up as a multibyte or not.


webmaster

Here is an easy way to get the header Content-Lenght.
<?
ob_start();
?>
Put HTML tags.
<?
$size=ob_get_length();
header("Content-Length: $size");
ob_end_flush();
?>


18-jan-2007 09:49

Dude, your web server will compute Content-length for you!

sinured

> Dude, your web server will compute Content-length for you!
I've noticed that Apache 1.3 doesn't do that.
However, if you’re running Apache 2.2 (and maybe 2.0, haven't experienced that yet) you don't need to worry about Content-Length.


Change Language


Follow Navioo On Twitter
flush
ob_clean
ob_end_clean
ob_end_flush
ob_flush
ob_get_clean
ob_get_contents
ob_get_flush
ob_get_length
ob_get_level
ob_get_status
ob_gzhandler
ob_implicit_flush
ob_list_handlers
ob_start
output_add_rewrite_var
output_reset_rewrite_vars
eXTReMe Tracker