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

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);

图片转代码服务由CSDN问答提供 功能建议

我编写了一个解析此文本文件的php脚本

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

一切都很好,但我希望控制下载的数量,即我不需要每一个结果可能最大的前5个。目前我正在下载整个文件(这是一个 浪费内存/带宽)。

我看到fopen有一个参数可以限制它,但是我放入的任何值都不会影响下载的文本量。 < 这可以吗? 感谢您阅读。

以下是有问题的代码片段,正在下载该文件。

 &lt;?php 
 
 $ file = fopen(“http://www.powerball.com/powerball/winnums-text.txt”,“rb”  ); 
 $ rows = array(); 
 
 while(!feof($ file))
 {
 $ line = fgets($ file); 
 $ date = explode(“绘制日期”,  $ line); 
 array_push($ rows,$ date [0]); 
 
} 
 
 
 nclclose($ file); 
 
?&gt; 
   
 
 

感谢大家这是只下载第一行结果的代码

  while(!feof($ file)  ))
 {
 $ line = fgets($ file); 
 $ date = explode(“Draw Date”,$ line); 
 array_push($ rows,$ date [0]); 
 
  if(count($ rows)&gt; 1)
 {
 break; 
} 
 
} 
 nclclose($ 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;
    
      }
    
    打赏 评论
  • doulun1939 2014-11-10 15:04

    The issue is that your while condition is only met once you've read through to the end of the file. If you only want to get the first N lines you'll need to change that condition. Something like this might help get you started:

    $lineCountLimit = 5;
    $currentLineCount = 0;
    while($currentLineCount < $lineCountLimit)
    {
        $line = fgets($file);
        $date = explode("Draw Date",$line);
        array_push($rows,$date[0]);
        $currentLineCount++;
    
    }
    
    打赏 评论
  • duanjia7912 2014-11-10 15:09

    Please try the following recipe to download only a part of the file, like 10 KBytes first, then split to lines and analyze them. How to partially download a remote file with cURL?

    打赏 评论

相关推荐 更多相似问题