Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post

    Detect if amount of time is in minutes?

    I have made a script to detect the amount of bytes roughly transferred per second which it uses to then estimate the amount of time which a file would take to download.

    Only problem is I don't know how to figure out if the amount of seconds is in minutes or hours :/ because for some reason there is always 1 dot in front of the number, it seems like if the estimated time is in the tens (min) then an "E" is added :S

    Examples:
    Estimated Download Time: 1.578
    Estimated Download Time: 7.93E

    PHP Code:
    <?php
    //Amount of time which page takes to load in microseconds.
    $time microtime();
    $time explode(" "$time);
    $time $time[0] + $time[1];

    //Function which can be used to get the current file size which script is contained, can also
    //calculate the amount of bytes which are downloaded based on the page load time and file size.
    function file_namesize($fun) {
        global 
    $time;
        if (
    $fun == "name") {
            
    $dir $_SERVER['PHP_SELF'];
            
    $filename strrchr($dir"/");
            
    $filename substr($filename1);
            return 
    $filename;
            unset(
    $dir,$filename);
        }
        elseif (
    $fun == "size") {
            
    $name file_namesize("name");
            
    $filesize filesize($name);
            return 
    $filesize;
            unset(
    $name,$filesize);
        }
        elseif (
    $fun == "bps") {
            
    $fsize file_namesize("size");
            
    $bps_f $fsize $time;
            
    $bps_f substr($bps_f05);
            return 
    $bps_f;
            unset(
    $fsize,$bps_f);
        }
    }

    //Display results
    echo "Page load time: ".$time."<br/>";
    echo 
    "Current Pagesize: ".file_namesize("size")." Bytes.<br/>";
    echo 
    "Filename: ".file_namesize("name")."<br/><br/>";
    echo 
    "Bytes Per Second (BPS): ".file_namesize("bps");

    //Below is the script which calculates how long a file will take to download based on the bytes per second
    //downloaded in above script.

    //Image location referenced below is the file which the script uses to estimate the estimated download time.
    $file1 "images/Reach_E310_Firefight_Beachhead04.jpg";
    //P100619_194537.jpg
    //katie edgell2.jpg

    //Estimate the download time for selected image.
    function est_dlt($file) {
        
    $bps file_namesize("bps");
        
    $file_size filesize($file);
        
    $result $bps $file_size;
        
    $get_dot strstr($result".");
        
    $count count($get_dot[0]);
        
    $result substr($result05);
        
        return 
    $result.", ".$count;
    }

    echo 
    "<br /><br /><br /><img src=\"".$file1."\" width=\"200px\"/><br />";
    echo 
    "Image Size: ".filesize($file1)." Bytes - Estimated Download Time: ".est_dlt($file1);
    ?>

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by martynball View Post
    I have made a script to detect the amount of bytes roughly transferred per second which it uses to then estimate the amount of time which a file would take to download.

    Only problem is I don't know how to figure out if the amount of seconds is in minutes or hours :/ because for some reason there is always 1 dot in front of the number, it seems like if the estimated time is in the tens (min) then an "E" is added :S

    Examples:
    Estimated Download Time: 1.578
    Estimated Download Time: 7.93E

    PHP Code:
    <?php
    //Amount of time which page takes to load in microseconds.
    $time microtime();
    $time explode(" "$time);
    $time $time[0] + $time[1];

    //Function which can be used to get the current file size which script is contained, can also
    //calculate the amount of bytes which are downloaded based on the page load time and file size.
    function file_namesize($fun) {
        global 
    $time;
        if (
    $fun == "name") {
            
    $dir $_SERVER['PHP_SELF'];
            
    $filename strrchr($dir"/");
            
    $filename substr($filename1);
            return 
    $filename;
            unset(
    $dir,$filename);
        }
        elseif (
    $fun == "size") {
            
    $name file_namesize("name");
            
    $filesize filesize($name);
            return 
    $filesize;
            unset(
    $name,$filesize);
        }
        elseif (
    $fun == "bps") {
            
    $fsize file_namesize("size");
            
    $bps_f $fsize $time;
            
    $bps_f substr($bps_f05);
            return 
    $bps_f;
            unset(
    $fsize,$bps_f);
        }
    }

    //Display results
    echo "Page load time: ".$time."<br/>";
    echo 
    "Current Pagesize: ".file_namesize("size")." Bytes.<br/>";
    echo 
    "Filename: ".file_namesize("name")."<br/><br/>";
    echo 
    "Bytes Per Second (BPS): ".file_namesize("bps");

    //Below is the script which calculates how long a file will take to download based on the bytes per second
    //downloaded in above script.

    //Image location referenced below is the file which the script uses to estimate the estimated download time.
    $file1 "images/Reach_E310_Firefight_Beachhead04.jpg";
    //P100619_194537.jpg
    //katie edgell2.jpg

    //Estimate the download time for selected image.
    function est_dlt($file) {
        
    $bps file_namesize("bps");
        
    $file_size filesize($file);
        
    $result $bps $file_size;
        
    $get_dot strstr($result".");
        
    $count count($get_dot[0]);
        
    $result substr($result05);
        
        return 
    $result.", ".$count;
    }

    echo 
    "<br /><br /><br /><img src=\"".$file1."\" width=\"200px\"/><br />";
    echo 
    "Image Size: ".filesize($file1)." Bytes - Estimated Download Time: ".est_dlt($file1);
    ?>
    I think that "E" is the mathematical notation for an exponential (base 10) multiplier. So following that logic, I would say that "7.93E" is actually 79.3 seconds, which is 1 minute and 19.3 seconds.

    If, for example, it were "7.93E5" it would be 793,000 seconds (7.93 x 10^5).

    Anyway, I'm pretty sure this is what it is, but I might be wrong. So check into it.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Actually, wouldnt this script be counting the page load time for the actual server as it is running the script. So it would not be detecting the users internet speed would it?

  • #4
    New Coder
    Join Date
    Jun 2009
    Location
    Manipal
    Posts
    45
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Ya, E denotes the exponent. To get the time in minutes, just divide by 60. The quotient would be the minutes and the remainder is seconds

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by martynball View Post
    Actually, wouldnt this script be counting the page load time for the actual server as it is running the script. So it would not be detecting the users internet speed would it?
    Thats correct. If you remove the substring call inside your est_dlt function, you could get a better idea of the time. My guess is that both of those numbers you have posted are actually E-X (that is, E to the negative x) numbers to reflect the $time specified by the server which will be fractions of a second. This is because your resultant has been further divided by the size in question, thus making this smaller than a normal number displayable with the 23 bits available in a float mantissa.

    To get the download speed from a client, you will need to connect to them. If you have exec based privileges, you can ping your client, capture the results of the ping and average out the packet size -> transfer ratio assuming that the upload reply is negligable. Alternatively, try establishing a socket connection with the client, and forcing some data down the pipe. Loop this for several records and draw an average.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fou-Lu View Post
    Thats correct. If you remove the substring call inside your est_dlt function, you could get a better idea of the time. My guess is that both of those numbers you have posted are actually E-X (that is, E to the negative x) numbers to reflect the $time specified by the server which will be fractions of a second. This is because your resultant has been further divided by the size in question, thus making this smaller than a normal number displayable with the 23 bits available in a float mantissa.

    To get the download speed from a client, you will need to connect to them. If you have exec based privileges, you can ping your client, capture the results of the ping and average out the packet size -> transfer ratio assuming that the upload reply is negligable. Alternatively, try establishing a socket connection with the client, and forcing some data down the pipe. Loop this for several records and draw an average.
    Don't know of any tutorials or example codes which show how to ping a client by any chance do you?

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Ping is simply done with either backticks or shell_exec:
    PHP Code:
    $iPingCount 4;
    $iPacketSize 512;
    $cmd sprintf('ping -c %d -s %d -q %s'$iPingCount$iPacketSize$userSuppliedIPAddress);
    // Windows server use:
    // $cmd = sprintf('ping -n %d -l %d %s', $iPingCount, $iPacketSize, $userSuppliedIPAddress);
    $exec shell_exec($cmd); 
    $exec will contain a number of lines, the last two are the most important. Depeding on OS depends on results, so you'll need to accommodate the formatting for each. Linux will look something like:
    Code:
    --- 127.0.0.1 ping statistics ---
    5 packets transmitted, 5 received, 0% packet loss, time 3998ms
    rtt min/avg/max/mdev = 0.047/0.053/0.061/0.009 ms
    And Windows something like:
    Code:
    Ping statistics for 127.0.0.1:
        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
        Minimum = 0ms, Maximum = 0ms, Average = 0ms
    What matters is the average for each. Some string manipulation should let you get those out (exploding and / or sscanf will do it for you).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    martynball (06-24-2010)

  • #8
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    my host has disabled shell_exec. Got an example I can work off using the backticks technique?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You can try using $exec = `$cmd`; instead. If thats disabled, another option is to open a socket to the user (which is not something I can actually test, but I can certainly try to write it).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #10
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    any chance you could give me a quick example of the other code so that I can read it and try to understand how it works?

    also, by using that code you just give me and the replacement it just gives me a blank screen, how do I view the results?

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    print $exec;.
    Here is an example of 'pinging' a machine in PHP using fsockopen. You can't actually ping it though; ICMP is not available for PHP to use.
    http://php.net/manual/en/function.fsockopen.php#47495
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    martynball (06-24-2010)

  • #12
    Regular Coder
    Join Date
    Nov 2007
    Posts
    682
    Thanks
    319
    Thanked 1 Time in 1 Post
    "print $exec" produces "$cmd". Also, are you saying I cannot ping the client then?

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oh wait, what am I thinking. Backticks are only enabled if shell_exec is enabled. Lol, so no goes there.
    Look into the socket connection instead.

    Oh, and yeah you cannot actually 'ping' a client with PHP and sockets; ICMP traffic is not supported by PHP. You can however simulate a ping by attempting to connect and determining the amount of time it takes to be rejected (hopefully rejected >.<), which is pretty much what the ping does anyway.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •