记录我工作学习生活中的点点滴滴......

测试下自己电脑用php下载远程文件的速度。
发布于 基础编程
2011-11-23 10:20:53
6551
5

本来想做一个txt下载站,将一个全文字站的小说名txt下载地址入库然后下载他们的txt到本地,这涉及到了php下载远程文件,做了个页面试了下,发现速度确实是不行,代码如下,有兴趣的朋友可以去试下,用的curl

<form method="post"> <input name="url" size="50" /> <input name="submit" type="submit" /> </form> <?php $starttime = time(); // maximum execution time in seconds set_time_limit(24 * 60 * 60); if (!isset($_POST['submit'])) { die(); } // folder to save downloaded files to. must end with slash $destination_folder = ''; $url = $_POST['url']; RunTime(); $newfname = $destination_folder . basename($url); $ch = curl_init(); $timeout = 100; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $contents = curl_exec($ch); curl_close($ch); $fp = fopen($newfname, 'w'); if ($fp) { flock($fp, LOCK_EX); fwrite($fp, $contents); fclose($fp); echo 'OK,File has been downloaded!<br>'; RunTime(); $mtime = explode(' ', microtime()); $size = filesize($newfname) / 1000; echo '大小:' . $size, 'K<bR>'; $time = ($mtime[1] + $mtime[0] - $starttime); echo '用时:' . $time, 'S<bR>'; $sudu = $size / $time; echo '速度:', $sudu . 'KB/S'; } function RunTime() { global $starttime; $mtime = explode(' ', microtime()); printf("Processed in %01.5f second(s)", $mtime[1] + $mtime[0] - $starttime); echo ' .<br />'; } ?>

分享本文到:
除非特殊注明,本文版权归原作者所有,欢迎转载!转载请注明版权以及本文地址,谢谢。
转载保留版权:Pakey's BLog >>基础编程 >>测试下自己电脑用php下载远程文件的速度。
本文地址:http://www.pakey.net/blog/403.html