Junior — Middle
How can efficient processing and data extraction from large files be performed?
sobes.tech AI
Answer from AI
For efficient processing and data extraction from large files in PHP, it is recommended to use stream reading to avoid loading the entire file into memory. For example, reading the file line by line using fgets() or in blocks with fread(). This allows processing data in parts and reduces memory load.
Example of line-by-line reading:
$handle = fopen('largefile.txt', 'r');
if ($handle) {
while (($line = fgets($handle)) !== false) {
// Process the line
echo $line;
}
fclose($handle);
} else {
// Error opening file
}
If the file has a structured format (e.g., CSV or JSON), specialized parsers that support streaming (like fgetcsv() for CSV) can be used. This allows extracting needed data efficiently without loading the entire file.