/** * @Title: Download * @Description: todo(下载) * @author laicaixia * @date 2013-6-2 上午10:30:49 * @throws */ public function Download() { $path = !empty($_REQUEST['path']) ? $_REQUEST['path'] : ""; @set_time_limit(C('PAGE_LIFE_TIME')); //设置该页面最久执行时间为300秒 import("@.ORG.RunTime"); import("@.ORG.HttpDownload"); $postfix = end(explode(".", $path)); /* if($path==$postfix or ($postfix!="zip" or $postfix!="Zip" or $postfix!="ZIP")){ echo "下载程序非ZIP文件,请检查下载文件!"; exit; }*/ $runtime = new RunTime(); $runtime->start(); #下载文件 $file = new HttpDownload(); # 实例化类 //$file->OpenUrl("http://www.ti.com.cn/cn/lit/an/rust020/rust020.pdf"); # 远程文件地址 $file->OpenUrl($path); //$file->OpenUrl("http://dl_dir.qq.com/qqfile/qq/QQ2010/QQ2010Beta3.exe"); # 远程文件地址 //重命名下载文件 $filename = "Upgrade.zip"; //$folder=date('Y-m-d'); //$folderdir=C('UPGRADE_PATH'); $aimDir = C('UPGRADE_PATH') . "/" . date('Y-m-d'); $file->SaveToBin($filename, $aimDir); // 保存路径及文件名 $file->Close(); # 释放资源 $runtime->stop(); echo "总下载时间:" . $runtime->spent(); }
private static function updateCache(callback $callback = null) { $cache = base::expand('app:/cache/geonames/'); if (!file_exists($cache)) { mkdir($cache, 0777, true); } $db = new DatabaseConnection(); // Pull the list of countries to update $rs = $db->getRows("SELECT * FROM geonames_datasets WHERE active=1"); foreach ($rs as $ci) { $download = false; if (!file_exists($cache . $ci['setkey'] . '.gz')) { $download = true; } else { $file = new HttpRequest($ci['url'], $cache . $ci['setkey'] . '.zip', array('method' => 'head')); $hdr = $file->headers(); $etaglocal = file_get_contents($cache . $ci['setkey'] . '.etag'); $etagremote = $hdr['ETag']; if ($etaglocal != $etagremote) { $download = true; } } if ($download) { cb($callback, 'Downloading ' . $ci['url'], 0, 1); $file = new HttpDownload($ci['url'], $cache . $ci['setkey'] . '.zip'); $hdr = $file->headers(); file_put_contents($cache . $ci['setkey'] . '.etag', $hdr['ETag']); cb($callback, 'Recompressing ' . $ci['setkey'] . '.gz', 0, 1); // Open input stream $fin = fopen('zip://' . $cache . $ci['setkey'] . '.zip#' . $ci['setkey'] . '.txt', 'r'); $fout = fopen('compress.zlib://' . $cache . $ci['setkey'] . '.gz', 'w'); stream_copy_to_stream($fin, $fout); fclose($fin); fclose($fout); cb($callback, 'Saved ' . $ci['setkey'] . '.gz'); } } // Update hierarchy $url = self::getUrl('hierarchy.zip'); if (file_exists($cache . 'hierarchy.etag')) { $hetag = file_get_contents($cache . 'hierarchy.etag'); } else { $hetag = null; } cb($callback, 'Updating hierarchy...', 0, 1); $head = new HttpRequest($url, array('method' => 'head')); $headers = $head->headers(); if ($hetag != $headers['ETag']) { file_put_contents($cache . 'hierarchy.etag', $headers['ETag']); $file = new HttpDownload($url, $cache . 'hierarchy.zip'); } // Open input stream $fin = fopen('zip://' . $cache . 'hierarchy.zip#hierarchy.txt', 'r'); $fout = fopen('compress.zlib://' . $cache . 'hierarchy.gz', 'w'); stream_copy_to_stream($fin, $fout); fclose($fin); fclose($fout); cb($callback, 'Saved hierarchy...'); }