PHP测试网站本地上传下载速度源码
发布:smiling 来源: PHP粉丝网 添加日期:2013-11-16 22:55:15 浏览: 评论:0
亲测一款php网速测试程序,超爽,平时都是在线测试本地上传和下载速度的,作为phper,看来真的是落伍了,php能做的程序功能还挺不少的,这是一款php测试网站本地上传下载速度的源码,只有一个文件,只运行一下就可以知道自己本地上传和下载的速度了,作为phper,经常使用php,可以将其制作成为一个小应用程序,放到桌面,想看本地上传下载速度时,只要运行一下就OK了,是不是很酷!
- <? header("Cache-Control:no-cache");
- //$self = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
- $self = $_SERVER['PHP_SELF'];
- if(isset($_POST['downTime'])&&isset($_POST['upTime'])){
- if(is_numeric($_POST['downTime'])&is_numeric($_POST['upTime'])){
- showResult();
- }else{
- showTest();
- }
- }elseif($_POST['action']=='uploadTest'){
- echo 'ok';
- }else{
- showTest();
- }
- function showTest(){?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTM
- L 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xh
- tml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
- <title>PHP--网速测试程序 测试正在进行中……</title>
- </head>
- <body>
- <table align="center" cellpadding="1" cellspacing="0">
- <tr>
- <td height="30" colspan="2" align="center"><b>下载速度测试中</b></td>
- </tr>
- <tr>
- <td width="300" style="border:1px solid blue"><table cellpadding="
- 0" cellspacing="0">
- <tr>
- <td id="downProgressBar" bgcolor="blue" style="width:0px; heigh
- t:17px"></td>
- </tr>
- </table></td>
- <td id="downProgressNum" align="right" width="35">0%</td>
- </tr>
- <form id="TestForm" action="" method="post"><input name="downTim
- e" id="downTime" type="hidden" /><input name="upTime" id="upTim
- e" type="hidden" /></form>
- </table>
- <script type="text/javascript">
- <!--
- function $id(id){
- return document.getElementById(id);
- }
- function setDownProgress(){
- Percentage++;
- dpb.style.width=(Percentage*3)+'px';
- dpn.innerHTML = Percentage+'%';
- }
- function setUpProgress(){
- Percentage+=10;
- upb.style.width=(Percentage*3)+'px';
- upn.innerHTML = Percentage+'%';
- if(Percentage==100){
- upTime =(new Date()).getTime() - upTime;
- submitTest();
- }
- }
- function XmlHttp(){
- try{
- if(window.XMLHttpRequest){
- var req = new XMLHttpRequest();
- if(req.readyState == null){
- req.readyState = 1;
- req.addEventListener("load", function(){
- req.readyState = 4;
- if(typeof req.onreadystatechange == "function")
- req.onreadystatechange();
- }, false);
- }
- return req;
- }
- if(window.ActiveXObject){
- return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
- }
- }
- catch(e){}
- }
- function getXmlHttpPrefix(){
- if(getXmlHttpPrefix.prefix)
- return getXmlHttpPrefix.prefix;
- var prefixes = ["MSXML2","Microsoft","MSXML","MSXML3"];
- var o;
- for(var i=0;i<prefixes.length;i++){
- try{
- o = new ActiveXObject(prefixes[i] + ".XmlHttp");
- return getXmlHttpPrefix.prefix = prefixes[i];
- }
- catch(ex){};
- }
- }
- function upTest(){
- var xmlHttp = XmlHttp();
- if(!xmlHttp)errorSubmit();
- xmlHttp.open('POST','<?=$GLOBALS['self']?>',true);
- xmlHttp.setRequestHeader("Content-Length",upBody.length);
- xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-ur
- lencoded");
- xmlHttp.onreadystatechange = function(){
- if(xmlHttp.readyState == 4){
- setUpProgress();
- }else if(xmlHttp.readyState == 3){
- upTest();
- }
- }
- xmlHttp.send(upBody);
- }
- function submitTest(){
- $id('downTime').value=downTime;
- $id('upTime').value=upTime;
- $id('TestForm').submit();
- }
- function errorSubmit(t){
- if(t)
- upTime = t;
- else
- upTime = 0;
- submitTest();
- }
- var dpb = $id("downProgressBar");
- var dpn = $id("downProgressNum");
- var upClickCount = Percentage = 0;
- var downTime =(new Date()).getTime();
- -->
- </script><?php
- $outText = getTestText();
- for($i = 1;$i<100;$i++){
- echo '<!--'.$outText."-->\n";
- echo "<script type=\"text/javascript\">setDownProgress();</scri
- pt>\n";
- }
- ?>
- <script type="text/javascript">
- var outstr = '<?=$outText?>';
- setDownProgress();
- downTime =(new Date()).getTime()-downTime;
- </script>
- <table align="center" cellpadding="1" cellspacing="0">
- <tr>
- <td height="30" colspan="2" align="center"><b>上转速度测试中</b></td>
- </tr>
- <tr>
- <td width="300" style="border:1px solid blue"><table cellpadding
- ="0" cellspacing="0">
- <tr>
- <td id="upProgressBar" bgcolor="blue" style="width:0px; height:17p
- x"></td>
- </tr>
- </table></td>
- <td id="upProgressNum" align="right" width="35">0%</td>
- </tr>
- </table>
- <script type="text/javascript">
- Percentage =0;
- var upb = $id("upProgressBar");
- var upn = $id("upProgressNum");
- var upBody='';
- for(i=0;i<10;i++)upBody +=outstr;
- upBody = 'action=uploadTest&content='+upBody;
- setTimeout('errorSubmit(-1)',300000);
- upTest();
- var upTime =(new Date()).getTime();
- </script>
- </body>
- </html><?php
- }
- function showResult(){?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTM
- L 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-t
- ransitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
- <title>PHP--测试结果</title>
- </head>
- <body>
- 说明,本程序测试的上传速度为上传到本站的速度<br /><br /><?php
- $dKBps = round(500000/$_POST['downTime'],2);
- $dKbps = $dKBps * 8;
- echo '下载速度:'.$dKbps.'Kbps,相当于'.$dKBps."KB/s<br />\n";
- if($_POST['upTime']=='-1'){
- echo '上转测试超时。';
- }else if($_POST['upTime']=='0'){
- echo '您的浏览器不支持本上传测试程序,请使用IE6.0以上或FireFox。';
- }else{
- $uKBps = round(500000/$_POST['upTime'],2);
- $uKbps = $uKBps * 8;
- echo '上传速度:'.$uKbps.'Kbps,相当于'.$uKBps.'KB/s';
- }
- echo '<br /><br /><br /><span style="padding-left:100px"><a href
- ="'.$GLOBALS['self'].'">再测一次</a></span>'?>
- </body>
- </html><?php
- }
- function getTestText(){
- $result;
- for($i = 0;$i<5000;$i++){
- $result .= '*';
- }
- return $result;
- }?>
Tags: PHP测试网站 上传下载
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)