Is it possible to know how many bytes sent to the client browser using php? My pages are created dynamically, so the size isn't fixed.
Using php's output buffering
// start output buffering
ob_start();
// create your page
// once the page is ready, measure the size of the output buffer
$length = ob_get_length();
// and emit the page, stop buffering and flush the buffer
ob_get_flush();
As usual with php, these functions are pretty well documented in the standard documentation, don't forget to read the user contributed notes.
You can see this in your webserver's access log file.
But you can also code some php to get an answer like this:
ob_start();
echo "your content"
$data = ob_get_contents();
$size = strlen($data);
see also: Measure string size in Bytes in php
I am using output buffering to process stuff (very descriptive, I know).
What I'd like to do is cause output buffering to end, have the handler functions perform their processing, and then get the resulting output as a string, not output to the browser.
Is this possible? I can't seem to work out what combination of ob_end/ob_get functions I would need to achieve this... and my initial thought of "I can just buffer the output of ob_end_flush" is... well, absurd :p
As a basic example of what I'm trying to achieve:
<?php
ob_start(function($c) {return "PASS\n";});
echo "FAIL\n";
$c = ob_get_contents();
ob_end_flush(); // outputs PASS, as it has been processed
echo $c; // outputs FAIL, because that was the content of the buffer... unprocessed...
// I want it processed.
I see the issue now with the edits and comments. You are correct that you'll need another output buffer since the callback is only called when flush or end is called and before any get in the function:
ob_start(function($c) {return "PASS\n";});
echo "FAIL\n";
ob_start();
ob_end_flush(); // buffers PASS, as it has been processed
$c = ob_get_contents();
echo $c; // outputs PASS
I'm having a hard time figuring out the problem in the following code, I really need a solution to this.
Consider the following code :
<?php
//starting a new output buffer, with a GZIP compression
ob_start("ob_gzhandler");
//this goes into the buffer
echo "Hi";
//grabbing the buffer's content
$content = ob_get_contents();
//cleaning the buffer
ob_clean();
//we're still inside the buffer, show the content again
echo $content;
This code fails to output "Hi", instead I see "‹óÈM", what have done that broke correct buffering? Knowing that once I remove "ob_gzhandler", the buffering is correct and everything is fine. I don't want to create another buffer and destroy the current one. I just want to clean the current one using ob_clean.
Any ideas? Thanks in advance.
Thank you for your answer, I figured out why, GZIP is insalled on my machine by the way, it's that when setting ob_gzhandler, the buffer is compressed chunk by chunk, so when using ob_get_contents(), parts of the last chunck are missing, and I end up getting weird output.
To correct that behaviour (or at least to bypass it), open a second output buffer, and leave the one with gzhandler() alone.
Like this
ob_start("ob_gzhandler");
ob_start();
Now the second one isn't compressed, I can do whatever I want with it (hence get its content, clean it etc). The content will be compressed anyway given that a higher level output buffer with gzhandler is opened.
Maybe you don't have gzip compression enabled/installed on your machine.
Tried your code and got something like that. I don't have gzip installed on my machine, try this:
It's your code but with a condition, if gzip doesn't start, the buffer starts.
//starting a new output buffer, with a GZIP compression
if (!ob_start("ob_gzhandler")) ob_start();
//this goes into the buffer
echo "Hi";
//grabbing the buffer's content
$content = ob_get_contents();
//cleaning the buffer
ob_clean();
//we're still inside the buffer, show the content again
echo "<pre>"; echo $content; echo "</pre>";
ob_end_flush();
If you get "Hi", maybe gzip is not installed.
Couldn’t find anything on the webs so here is the issue: I have a cropper tool and I want to show the cropped image on this page. But because my functions.php has a function that uses a header method, I had to use ob_start in my file. That causes the problem that my image is not shown (it’s a question mark right now, not the right image).
Code:
<?php
ob_start();
require_once("includes/session.php");
require_once("includes/connection.php");
require("includes/constants.php");
require_once("includes/functions.php");
confirm_logged_in();
require_once("includes/header.php");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$targ_w = $_POST['w'];
$targ_h = $_POST['h'];
$jpeg_quality = 90;
$src = $_POST['image'];
$ext = end(explode(".", $_POST['image']));
switch($ext) {
case 'jpg';
$img_r = imagecreatefromjpeg($src);
$dst_r = imagecreatetruecolor($targ_w, $targ_h);
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
header('Content-type: image/jpeg');
imagejpeg($dst_r,null, $jpeg_quality);
$output = ob_get_contents();
break;
case 'png';
$img_r = imagecreatefrompng($src);
$dst_r = ImageCreateTrueColor($targ_w, $targ_h);
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
header('Content-type: image/png');
imagepng($dst_r, null, 8);
$output = ob_get_contents();
break;
}
}
echo $output;
ob_end_clean();
?>
Given how your code is indendent:
<?php ob_start(); ?>
<?php require_once("includes/session.php"); ?>
[...snip...]
$targ_h = $_POST['h'];
Those 4 spaces before the <?php ob_start call are output and disabling your subsequent header() calls.
Plus, nowhere do you actually OUTPUT your image data:
$output = ob_get_contents();
echo $output; // <----you need this
When outputting binary files, make sure you output only the data you want to:
The first thing in your code should be <?php and you should never leave the PHP code. No ?> should be used, more below.
When using Unicode in your script, be sure not to include BOM.
Do not use the closing ?>. It is not necessary. If you use it, anything after that is sent to output. This includes a newline character at the end of the last line of the script.
If your code is designed poorly and generates any garbage before outputting the data, buffer the garbage generation and then throw away the buffer just before outputting the data.
Now to your code specifically:
In switch statement, the case part is ended by a colon, not semicolon. I.e. write case 'jpg': instead of case 'jpg';.
The ob_end_clean(); at the end of your script should be moved right after the last require_once call. The includes are producing garbage, the rest of the output is wanted; therefore you should buffer only the output generated by the includes and then throw the buffer away and let the rest be unbuffered.
Delete the lines $output = ob_get_contents(); (occurs twice) and echo $output;. After performing the previous change, they are not needed anymore, they just produce errors.
Try it and see if it helps. If not, comment on this answer and we’ll try to find your problem.
ob_start starts output buffering. ob_end_clean cleans the buffer and stops output buffering without sending anything to the client, so you basically discard any output.
I think you meant to use ob_end_flush instead of ob_end_clean, which sends the output buffer to the client instead of just ending buffering.
Since you used ob_get_contents to put the output in a variable, you could opt to echo that variable after calling ob_end_clean, but that will make your script just larger, less clear and more memory-consuming, since you then have the contents of the entire image in the output buffer and in the $output variable. So I think using ob_end_flush really is the better option.
update ur code like this:
......
switch($ext)
{
case 'jpg';
ob_start();
......
cannot have output before header().
aha, my English sucks.
But because my functions.php has a function that uses a header method,
I had to use ob_start in my file. That causes the problem that my
image is not shown (it’s a question mark right now, not the right
image).
Both statements are not really true. First: if you have a function that uses a header method, don't worry, as long as the function is not executed, your script doesn't care about the header method. And if it gets executed, ob_start wont help, because
While output buffering is active no output is sent from the script
(other than headers), instead the output is stored in an internal
buffer.
(http://www.php.net/ob_start) Note the "other than headers". So the real problem is not you have a header method, but that you have some output in one of your includes, either a whitespace or any other output, e.g. "includes/header.php" sounds like it may output the html header.
So first thing would be to remove all output from these files, and then remove all output buffering functions from your script.
So really you don't need output buffering, but even if you would need it, output buffering is not the cause that your image is not shown, output buffering works fine, but in your code you output the image before ob_end_clean, thus discarding any output.
And if you really by no means can remove the output from the includes, just call ob_end_clean right after the includes and continue as usual without output buffering.
I want to copy the file http://searchr.us/Testing/web-search.phtml?search=SEARCHED+TEXT to
http://searchr.us/Testing/search/SEARCHED+TEXT.html
How do i do this?
NOTE:The source of http://searchr.us/Testing/search/SEARCHED+TEXT.html should be the same as http://searchr.us/Testing/web-search.phtml?search=SEARCHED+TEXT
Indirectly I'm just saving a query so that I can keep a track of them!
Hope i've get you right: you want to store output of any query into file. You can manage this with output buffering.
In the begining of script wright:
ob_start(); // start buffering
This turns buffering on.
At the eng of page wright something like this ($query is text of query):
$html = ob_get_contents();
ob_end_flush(); // this turns off buffering and sends buffered content to output
$fp = fopen("{$query}.html","w");
fwrite($fp,$html);
fclose($fp);