首页 > Web开发 > 详细

php下载文件,解压文件,读取并写入新文件

时间:2015-11-13 18:46:50      阅读:406      评论:0      收藏:0      [点我收藏+]

以下代码都是本人在工作中遇到的问题,并完成的具体代码和注释,不多说,直接上代码:

<?php
     //组织链接
     $dataurl = "http://118.194.236.54:888/kw/";
     $date = date("Y-m-d",strtotime("-1 day"));
     $fileName = $date . ".tar.gz";
     $dataurl = $dataurl . $fileName;

     //下载昨天数据,如果curl方法可用,默认使用curl方法!
     function httpcopy($url, $file="", $timeout=60)
     {
         $file = empty($file) ? pathinfo($url, PATHINFO_BASENAME) : $file;
         $dir = pathinfo($file, PATHINFO_DIRNAME);
         !is_dir($dir) && @mkdir($dir, 0755, true);
         $url = str_replace(" ", "%20", $url);

         if(function_exists(‘curl_init‘)) {
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_URL, $url);
             curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
             $temp = curl_exec($ch);
             if(@file_put_contents($file, $temp) && !curl_error($ch)) {
                 return $file;
             } else {
                 return false;
             }
         } else {
             $opts = array(
                 "http" => array(
                 "method" => "GET",
                 "header" => "",
                 "timeout" => $timeout)
             );
             $context = stream_context_create($opts);
             if(@copy($url, $file, $context)) {
                 return $file;
             } else {
                 return false;
             }
         }
     }

     if(!httpcopy($dataurl, "./" . $fileName, 60)) {
          echo "下载出错!";
     }

     //解压tar.gz文件
     $phar = new PharData($fileName);
     $phar->extractTo(‘./‘, null, true);


     //读取解压后的文件数据
     $handle = fopen($date . ".txt", "r");
    
     $text = ‘‘;
     while (!feof($handle)) {
         $buffer = fgets($handle);
         $res = explode("\t", $buffer);
         $text .= $res[1] . ‘ ‘;
     }
     fclose($handle);

     //写入关键字文件
     $name = $date . "keyword.txt";
     $fp = fopen($name, "w+");
     fwrite($fp, $text);
     fclose($fp);
    
?>

 

php下载文件,解压文件,读取并写入新文件

原文:http://www.cnblogs.com/xjser/p/4962901.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!