Suddenly encountered a demand to read more than 80 m files, it is a tragedy, whether it is file_get_content or file, will load the read files to the memory at one time
Suddenly encountered a demand to read more than 80 m files, it is a tragedy, whether it is file_get_content or file, will load the read files to the memory at one time
Under normal circumstances, we can use fseek to read data. The advantage is that it will not be read at once. The following code is only applicable 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: // jb51.net'. $ 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 );
, Hong Kong virtual host, website space, US space