douaoli5328 2016-04-23 22:27
浏览 95

设置CSV文件行读取限制

I have a script that reads csv files and return them as an array. It reads many different files with different content, number of rows and columns and with different size (ex: from <1kb to >1Gb).

$data = [];
$file = fopen($file, 'r');
while (($line = fgetcsv($file)) !== FALSE  ) {
    $data[] = $line;
}
fclose($file);

I am looking for an elegant way to make sure that when it comes across large files it does not go over the whole file in order to avoid an excessive workload.

Here are some ideas :

  1. Stopping after n rows. However, for a large file that have few rows and several columns, that wouldn't help

  2. Read rows based on the size. Ex: Read all the rows of a 500Mb file but only 50% of the rows of a 1Gb file. However, if the last rows are a bunch of empty rows or if they are more columns in the first rows, that solution wouldn't help.

  3. Monitor the size of $data and stop after a certain threshold. I do not have any rational for excluding this idea except that I am not sure that it is a common way of doing things

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 蓝桥oj3931,请问我错在哪里
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染