0

这是我的计划:我的网页是一个简单的文件共享系统。我想为用户显示下载速度。这不是100%,但它相对较好。我想写下下载的时间......例如:你的下载速度是300kb / s,你可以在7秒内下载这个文件..


我有 2 个 PHP 文件。

阿尔法文件这样做:

ob_start();
require 'speedtest.php';
$sebesseg = ob_get_clean();

这很简单。我从 speedtest.php 中只得到一个数字我的问题是:我有一个变量:(int)$size = 1; 我想做他的:$time_left = $size / $sebesseg; $sebesseg指速度。以字节为单位的下载速度。但是我不能使用 settype 或(int)$sebesseg.. 或任何我已经知道的东西,因为它给我写了一个空变量.. :-( 我该如何解决这个问题?

4

2 回答 2

1

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 文档相同。

于 2013-01-28T17:44:47.737 回答
0

使用函数stream_notification_callback()

例子:

function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {

    switch($notification_code) {
        case STREAM_NOTIFY_RESOLVE:
        case STREAM_NOTIFY_AUTH_REQUIRED:
        case STREAM_NOTIFY_COMPLETED:
        case STREAM_NOTIFY_FAILURE:
        case STREAM_NOTIFY_AUTH_RESULT:
            var_dump($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max);
            /* Ignore */
            break;

        case STREAM_NOTIFY_REDIRECTED:
            echo "Being redirected to: ", $message;
            break;

        case STREAM_NOTIFY_CONNECT:
            echo "Connected...";
            break;

        case STREAM_NOTIFY_FILE_SIZE_IS:
            echo "Got the filesize: ", $bytes_max;
            break;

        case STREAM_NOTIFY_MIME_TYPE_IS:
            echo "Found the mime-type: ", $message;
            break;

        case STREAM_NOTIFY_PROGRESS:
            echo "Made some progress, downloaded ", $bytes_transferred, " so far";
            break;
    }
    echo "\n";
}

$ctx = stream_context_create();
stream_context_set_params($ctx, array("notification" => "stream_notification_callback"));

file_get_contents("http://php.net/contact", false, $ctx);
于 2022-01-18T09:44:45.350 回答