dongzhan1383 2014-11-10 14:57
浏览 55
已采纳

PHP使用fopen通过URL下载txt文件。 如何限制下载量?

I have written a php script which parses this text file

http://www.powerball.com/powerball/winnums-text.txt

Everything is good, but I wish to control the amount that is download i.e. I do not need every single result maybe max the first 5. At the moment I am downloading the entire file (which is a waste of memory / bandwidth).

I saw the fopen has a parameter which supposed to limit it but whatever value I placed in has no effect on the amount of text that is downloaded.

Can this be done? Thank you for reading.

Here is a small snippet of the code in question which is downloading the file.

<?php

$file = fopen("http://www.powerball.com/powerball/winnums-text.txt","rb");
$rows = array();

    while(!feof($file))
    {
        $line = fgets($file);
        $date = explode("Draw Date",$line);
        array_push($rows,$date[0]);

    }


fclose($file);

?>

Thanks everyone this is the code which just downloads the first row of results

   while(!feof($file))
        {
            $line = fgets($file);
            $date = explode("Draw Date",$line);
            array_push($rows,$date[0]);

            if(count($rows)>1)
            {
                break;
            }

        }
fclose($file);
  • 写回答

3条回答 默认 最新

  • drqrdkfue521903877 2014-11-10 15:01
    关注

    You can break whenever you don't need more data. In this example when count($rows)>100

     while(!feof($file)) {
            $line = fgets($file);
            $date = explode("Draw Date",$line);
            array_push($rows,$date[0]);
    
           if (count($rows)>100)
              break;
    
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误