Measure UPLOAD speed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Snap
Forum Newbie
Posts: 3
Joined: Thu Feb 05, 2009 8:11 am

Measure UPLOAD speed

Post by Snap »

How can i measure a user UPLOAD speed ...

i have this script, who can measure only download speed, but what i really need is to know the UPLOAD speed, is there a way to do that ?

Code: Select all

 
<?php
$kb=2048;
echo "streaming $kb Kb...<!-";
flush();
$time = explode(" ",microtime());
$start = $time[0] + $time[1];
for($x=0;$x<$kb;$x++){
    echo str_pad('', 1024, '.');
    flush();
}
$time = explode(" ",microtime());
$finish = $time[0] + $time[1];
$deltat = $finish - $start;
echo "-> Test finished in $deltat seconds. Your speed is ". round($kb / $deltat, 3)."Kb/s";
?>
 
many thanks !!

pd. i don't know much about php, i'm a Java programmer, so please be nice with me :)
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Re: Measure UPLOAD speed

Post by mchaggis »

Sorry to put a dampener on this topic, but...

First of all your download script wouldn't work as it would just flush as quick as possible. The script will continue to run through and finish even if the client has not yet fully downloaded.

The easiest way of doing an upload test, would be to use a combination of PHP and javascript (in the form of AJAX). Something along the lines of the following:

You will need 3 files:
1) An upload form
2) A start the timer script
3) A results form

The upload form: (I'm gonna use a jquery ajax call as it's less to type)

Code: Select all

 
<script>
function startUpload() {
    $.get('starttimer.php',{},function () {
        document.getElementById('myform').submit();
    });
    return false;
}
</script>
<form enctype="multipart/form-data" action="results.php" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" onclick="startUpload(); return false;" />
</form>
 
 
The start the timer script, just needs to set a session and log the time:

Code: Select all

 
<?php
session_start();
$_SESSION['uploadStartTime'] = mktime();
?>
 
The results form would be something like:

Code: Select all

 
<?php
session_start();
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
    print "The number of seconds taken to upload {$_FILES['userfile']['size']}bytes was ";
    print (mktime() - $_SESSION['uploadStartTime']);
}
?>
 
Now, the above code may or may not work out of the box, but it should give you a starting point. Basically, you make an ajax call to a script to make a note of the time on the server immediately prior to the upload beginning. That way you have something to compare against when the upload is complete, as the php script will not get executed until the upload has been completed.

Hope that helps
Snap
Forum Newbie
Posts: 3
Joined: Thu Feb 05, 2009 8:11 am

Re: Measure UPLOAD speed

Post by Snap »

of course it helps !!!

many thanks man :)

if i could make something good, i will post it for future users ...

anyway, i am still open to others solutions (java applet with php maybe) or etc
Snap
Forum Newbie
Posts: 3
Joined: Thu Feb 05, 2009 8:11 am

Re: Measure UPLOAD speed

Post by Snap »

$_SESSION['uploadStartTime'] seem not to work ... :cry:

maybe is something wrong with

function startUpload() {
$.get('starttimer.php',{},function () {
document.getElementById('myform').submit();
});
return false;
}
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Re: Measure UPLOAD speed

Post by mchaggis »

Does it work if you just access the starttimer.php script directly?
Post Reply