0

Here is what I'm planning: My webpage is a simple filesharing system. I would like to show the download speed for the user. It is not 100%, but it's relative good. And i would like to write the time for downloading... example: your download speed is 300kb/s, you can download this file in 7 seconds..


I have got 2 PHP files.

Alfa file do this:

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

This is simple. I get only one number from the speedtest.php My problem is: I have a variable: (int)$size = 1; I would like to do his: $time_left = $size / $sebesseg; $sebesseg means speed. Download speed in bytes. But I can't use settype, or (int)$sebesseg .. or anything I already know, 'cos it wrotes me an empty variable.. :-( How can I solve this?

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
janos
  • 247
  • 2
  • 4
  • 11
  • what is the output of `var_dump($sebesseg)`? – Fabian Schmengler Jan 28 '13 at 17:42
  • 347347 with 2 spaces at the end? :-o ... (string) – janos Jan 28 '13 at 17:45
  • what? a string with 1975 characters? and you want to cast *that* to int? It seems like `speedtest.php` does not what you expect it to do. – Fabian Schmengler Jan 28 '13 at 17:49
  • @fab Thats not about speedtest.php . ob_get_clean() will return a string. check my answer – hek2mgl Jan 28 '13 at 17:55
  • 1
    of course it returns a string. But the OP assumed, it contained the download speed in bytes, while you assumed it returns the payload of the speed test. Too much assumption going on here to my taste ;) – Fabian Schmengler Jan 28 '13 at 18:08
  • `require` is a poor man's function call. Learn about functions and you should be able to do this in a much clearer way. – Halcyon Jan 28 '13 at 17:42
  • My problem is. I use Javascript in the speedtes.php :-) And I don't know how to get variable from Javascript to. It's may be easyer this way... Not? .. I just don't know what is the problem... – janos Jan 28 '13 at 17:43
  • This is not how you get a value in PHP from JavaScript. PHP runs before anything is sent to the client. JavaScript runs after that. Look into `AJAX`/`XMLHTTPRequest`. – Halcyon Jan 28 '13 at 17:45

2 Answers2

1

ob_get_clean() will return a string. To obtain the number of bytes write

$sebesseg = ob_get_clean();
$numberOfBytes = strlen($sebesseg);

After reading your last comment, I've preapred a short example how a simple download speed measurement script can be done with PHP. The following code should do what you want:

<?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]; 
}

Note that the real download speed can only measured at the client. But the results from the code above should be approximately ok.

Also it would just measure the download size of the HTML page itself. Images. styles and javascripts will extend the real download size of page load. But the speed should be in most cases the same the HTML document.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • It give me only the same number everytime.. This is my php output: http://boxy.tigyisolutions.hu/speedtest.php Yeah. It gives to me 1975... :-s – janos Jan 28 '13 at 18:15
  • @user1978100 I get a different output on every site load. What are you really planning? – hek2mgl Jan 29 '13 at 00:30
  • My webpage is a simple filesharing system. I would like to show the download speed for the user. It is not 100%, but it's relative good. And i would like to write the time for downloading... example: your download speed is 300kb/s, you can download this file in 7 seconds... – janos Jan 29 '13 at 13:54
  • I think that would give the bytes per milliseconds. I would suggest using `time()` instead of `microtime()` or convert the milliseconds to seconds after calculating the difference. – Kamal Khan Sep 13 '19 at 15:58
  • @KamalKhan `microtime(true)` will give you a float: `seconds.useconds`. It's just more accurate than `time()`. – hek2mgl Sep 13 '19 at 16:49
0

use function stream_notification_callback()

Example:

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);
miaotiao
  • 1
  • 2