本来想做一个txt下载站,将一个全文字站的小说名txt下载地址入库然后下载他们的txt到本地,这涉及到了php下载远程文件,做了个页面试了下,发现速度确实是不行,代码如下,有兴趣的朋友可以去试下,用的curl
$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!
';
RunTime();
$mtime = explode(' ', microtime());
$size = filesize($newfname) / 1000;
echo '大小:' . $size, 'K
';
$time = ($mtime[1] + $mtime[0] - $starttime);
echo '用时:' . $time, 'S
';
$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 ' .
';
}
?>
分享本文到: