How to read large files in limited memory in php

Source: Internet
Author: User
A sudden demand for reading more than 80 MB of files is very tragic. whether it is file_get_content or file, it will load the read files to the memory at one time. normally, we can use fseek to read data. the advantage is that it does not read data at once. the following code is applicable only to the processing of edge fetch and is not suitable for one-time reading and processing.
You can generate a test file using the following methods:
The code is as follows:
$ File_handle = fopen ("./csdn.txt", "rb + ");
For ($ index1 = 1; $ index1 <= 2000000; $ index1 ++ ){
Fwrite ($ file_handle, 'http: // bitsCN.com '. $ index1. "\ r ");
}
Fclose ($ file_handle );

The read processing code is as follows:
The code is as follows:
$ I = 0;
$ Now = '';
While ($ I> = 0 ){
If ($ I> 10 ){
Break;
}
Fseek ($ file_handle, 0, SEEK_CUR );
$ Now = fgetc ($ file_handle); // you can write a code to determine whether the value is false.
If ($ now = "\ r "){
Echo 'locate the checkpoint ';
}
Echo $ now;
$ I ++;
}
Fclose ($ file_handle );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.