How to read big file in php without being memory limit

前端 未结 4 1662
青春惊慌失措
青春惊慌失措 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:18

    You can use readfile and ob_get_level to manage memory and output buffering.

    readfile() will not present any memory issues, even when sending large files, on its own. If you encounter an out of memory error ensure that output buffering is off with ob_get_level().

    You may still have PHP output buffering active while performing the read.

    Check that with:

    You can use the following at the start of your script to stop output buffering if it's already started:

    if (ob_get_level()) {
      ob_end_clean();
    }
    

提交回复
热议问题