ob_get_clean()
将返回一个字符串。获取写入的字节数
$sebesseg = ob_get_clean();
$numberOfBytes = strlen($sebesseg);
在阅读了您的最后一条评论后,我准备了一个简短的示例,如何使用 PHP 完成一个简单的下载速度测量脚本。下面的代码应该做你想做的事:
<?php
// get the start time as UNIX timestamp (in millis, as float)
$tstart = microtime(TRUE);
// start outout buffering
ob_start();
// display your page
include 'some-page.php';
// get the number of bytes in buffer
$bytesWritten = ob_get_length();
// flush the buffer
ob_end_flush();
// how long did the output take?
$time = microtime(TRUE) - $tstart;
// convert to bytes per second
$bytesPerSecond = $bytesWritten / $time;
// print the download speed
printf('<br/>You\'ve downloaded %s in %s seconds',
humanReadable($bytesWritten), $time);
printf('<br/>Your download speed was: %s/s',
humanReadable($bytesPerSecond));
/**
* This function is from stackoverflow. I just changed the name
*
* http://stackoverflow.com/questions/2510434/php-format-bytes-to-kilobytes-megabytes-gigabytes
*/
function humanReadable($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
// Uncomment one of the following alternatives
//$bytes /= pow(1024, $pow);
$bytes /= (1 << (10 * $pow));
return round($bytes, $precision) . ' ' . $units[$pow];
}
请注意,实际下载速度只能在客户端测量。但是上面代码的结果应该差不多。
它也只会测量 HTML 页面本身的下载大小。图片。样式和 javascripts 将扩展页面加载的实际下载大小。但速度在大多数情况下应该与 HTML 文档相同。