douwen4125 2014-06-21 09:07
浏览 104

从网址下载自动zip

I need help with one snippet of code that has me ready to explode, involving an automatically downloaded zip folder for an external URL, containing just a single txt file, that I will upload to a mysql database, to complete a project I've been working on for five years.

The short version is I need to get the daily text file from this url:

http://batstrading.com/market_data/shortsales/2014/06/BYXXshvol20140620.txt.zip-dl?mkt=byx

I have tried everything, in a hundred combinations. On those occasions no errors occurred, there were no txt files created nor uploaded to the db , permissions set to 777

zip_open, zip_read, zip_entry_open, zip_entry_read, fgets, fopens, file_get_contents

The long version with a valid url is:

function getBATStxtfile($BATSzipurlFile, $BATSZoutputFile)
{  
    $BATSzipurl="http://batstrading.com/market_data/shortsales/2014/06/BYXXshvol20140620.txt.zip-dl?mkt=byx";  

    file_get_contents($BATSzipurl); 

    $batstxtzipFile=zip_open($BATSzipurl);  
    $batstxtzipFile=zip_read($BATSzipurl);  
    $batstxtFile = zip_entry_open($batstxtzipFile;  
    $batstxt = zip_entry_read($batstxtFile);  
    $batstxt = str_replace("Date|Symbol|Short Volume|Total Volume|Market Center", "",$batstxt);  
    $batstxt = str_replace("|",  ",", $batstxt);  
    $batstxt = trim($batstxt);  

    file_put_contents($BATSZoutputFile, $batstxt);

    zip_entry_close($batstxtFile);  
    zip_close($BATSzipurl);  
}

I have 3000000 lines of code, 10,000 failed scripts, and all I need to have my life back...is this .txt file retrieved. Thank you in advance.

  • 写回答

1条回答 默认 最新

  • douwei8295 2014-06-21 09:47
    关注

    Here you go

    <?php
    
    // fetch zip file
    $zip = file_get_contents('http://batstrading.com/market_data/shortsales/2014/06/BYXXshvol20140620.txt.zip-dl?mkt=byx');
    file_put_contents('bats.zip', $zip);
    
    // read specific file from zip archive
    $txt = file_get_contents('zip://bats.zip#BYXXshvol20140620.txt');
    
    // do text transformations
    $txt = str_replace('Date|Symbol|Short Volume|Total Volume|Market Center', '', $txt);
    $txt = str_replace('|',  ',', $txt);
    $txt = trim($txt);
    
    // output the modified txt to file
    file_put_contents('out.txt', $txt);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?