首先放置这段代码到 /source/include/cron/ 目录下面,命名为 alexaDownload.php
- <?php
- # author: 炎藤
- # website: cnBayarea.com
- $thisYear = date("Y");
- $dateToday = date("Ymd");
- $url = "http://s3.amazonaws.com/alexa-static/top-1m.csv.zip";
- $path = "./alexa/".$thisYear."/".$dateToday.".zip";
- $zip = new ZipArchive;
- $newName = $dateToday.".txt";
- $zipDir = "./alexa/".$thisYear."/";
- // create folder
- if(!is_dir($thisYear)){
- if(!mkdir($thisYear, 0777)) {
- die('Failed to create folders...');
- };
- }
- // download
- if(!is_file($path)){
- $ch = curl_init($url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $data = curl_exec($ch);
- curl_close($ch);
- file_put_contents($path, $data);
- }
- // unzip
- // unzip 1/2 - rename file
- if ($zip->open($path)) {
- $zip->renameName('top-1m.csv', $newName);
- $zip->close();
- } else {
- echo 'fail to open zip file';
- }
- // unzip 2/2 - unzip file
- if ($zip->open($path)) {
- $zip->extractTo($zipDir);
- } else {
- echo 'fail to unzip file';
- }
- ?>
复制代码
这段代码会把文件下载到 /alexa/ 目录下,并按时间日期来排列
实例: http://cnbayarea.com/alexa/2012/1201.txt
|
上一篇: PHP自定义的Unicode编码转GBK,GBK转Unicode编码函数下一篇: 1314学习网签到记录贴-2012年12月3日
|