How to read big file in php without being memory limit

前端 未结 4 1637
青春惊慌失措
青春惊慌失措 2021-01-14 05:04

I\'m trying to read a file line by line. The problem is the file was too big(over 500000 line) and I reach out the memory limit. I wonder how to read the file without being

4条回答
  •  悲&欢浪女
    2021-01-14 05:23

    You can set the memory limit like ini_set('memory_limit',-1);//You script will not stoped until its finished the reading. but this is wrong way beacuse its take your CPU utilization time on server.

    Better is to divide the file in chunks,

    covert you file data to array then you can easily read it in chunks like

    $file_lines = file('mytext.txt');
    foreach ($file_lines as $line) {
        echo $line;
    }
    

    $file_lines is your array.

提交回复
热议问题