Navigation

Php

How to Read a File Line by Line with fopen()

Process large files efficiently by reading them line by line using fopen() - perfect for handling files that don't fit in memory.

Table Of Contents

Here's How to Do It

<?php

// Basic line-by-line reading
function readFileByLine(string $filename): void {
    $handle = fopen($filename, 'r');
    
    if (!$handle) {
        throw new RuntimeException("Cannot open file: $filename");
    }
    
    while (($line = fgets($handle)) !== false) {
        // Process each line
        echo "Line: " . trim($line) . "\n";
    }
    
    fclose($handle);
}

// Memory-efficient CSV processing
function processCsvFile(string $filename): array {
    $results = [];
    $handle = fopen($filename, 'r');
    
    if (!$handle) {
        return $results;
    }
    
    // Skip header row
    $header = fgetcsv($handle);
    
    while (($data = fgetcsv($handle)) !== false) {
        if (count($data) === count($header)) {
            $results[] = array_combine($header, $data);
        }
    }
    
    fclose($handle);
    return $results;
}

// Log file analysis
function analyzeLogFile(string $logFile, string $searchPattern): array {
    $matches = [];
    $lineNumber = 0;
    
    $handle = fopen($logFile, 'r');
    if (!$handle) {
        return $matches;
    }
    
    while (($line = fgets($handle)) !== false) {
        $lineNumber++;
        
        if (str_contains($line, $searchPattern)) {
            $matches[] = [
                'line' => $lineNumber,
                'content' => trim($line),
                'timestamp' => substr($line, 0, 19) // Assuming log format
            ];
        }
    }
    
    fclose($handle);
    return $matches;
}

// Configuration file reader with comments
function parseConfigFile(string $configFile): array {
    $config = [];
    $handle = fopen($configFile, 'r');
    
    if (!$handle) {
        return $config;
    }
    
    while (($line = fgets($handle)) !== false) {
        $line = trim($line);
        
        // Skip empty lines and comments
        if ($line === '' || $line[0] === '#') {
            continue;
        }
        
        // Parse key=value pairs
        if (str_contains($line, '=')) {
            [$key, $value] = explode('=', $line, 2);
            $config[trim($key)] = trim($value);
        }
    }
    
    fclose($handle);
    return $config;
}

// Safe file reader with error handling
function safeLineReader(string $filename, callable $processor): bool {
    $handle = @fopen($filename, 'r');
    
    if (!$handle) {
        error_log("Failed to open file: $filename");
        return false;
    }
    
    try {
        $lineNumber = 0;
        while (($line = fgets($handle)) !== false) {
            $lineNumber++;
            
            try {
                $processor($line, $lineNumber);
            } catch (Exception $e) {
                error_log("Error processing line $lineNumber: " . $e->getMessage());
                continue;
            }
        }
    } finally {
        fclose($handle);
    }
    
    return true;
}

// Usage examples
try {
    readFileByLine('data.txt');
} catch (RuntimeException $e) {
    echo "Error: " . $e->getMessage();
}

// Process CSV data
$csvData = processCsvFile('users.csv');
foreach ($csvData as $user) {
    echo "User: {$user['name']} - Email: {$user['email']}\n";
}

// Analyze logs
$errorLogs = analyzeLogFile('app.log', 'ERROR');
echo "Found " . count($errorLogs) . " error entries\n";

// Parse configuration
$config = parseConfigFile('app.conf');
echo "Database host: " . ($config['db_host'] ?? 'localhost') . "\n";

// Safe processing with callback
safeLineReader('large-data.txt', function($line, $lineNumber) {
    if ($lineNumber % 1000 === 0) {
        echo "Processed $lineNumber lines\n";
    }
    // Process line data here
});

Why This Approach Works

Line-by-line reading with fopen() provides several advantages:

  1. Memory Efficiency: Only loads one line at a time, not the entire file
  2. Large File Handling: Can process files larger than available RAM
  3. Real-time Processing: Start working with data immediately
  4. Error Recovery: Can handle and skip problematic lines

Key Functions:

  • fopen($filename, 'r') - Opens file for reading
  • fgets($handle) - Reads one line including newline
  • fgetcsv($handle) - Reads and parses CSV line
  • fclose($handle) - Always close the file handle

Perfect for processing logs, CSV files, configuration files, and any large text-based data where memory usage matters.

Share this article

Add Comment

No comments yet. Be the first to comment!

More from Php