Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
<?php $path = '/var/data/config.txt'; // Read entire file as a string if (file_exists($path)) { $contents = file_get_contents($path); echo $contents; } // Read into an array of lines (FILE_IGNORE_NEW_LINES strips ) $lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { echo trim($line) . " "; } // Low-level fopen for large files (read line by line) $handle = fopen($path, 'r'); if ($handle === false) { throw new RuntimeException("Cannot open file: $path"); } try { while (($line = fgets($handle)) !== false) { echo trim($line) . " "; } } finally { fclose($handle); } // Read a specific number of bytes $handle = fopen($path, 'r'); $chunk = fread($handle, 1024); // read first 1 KB fclose($handle); // Read remote URL (if allow_url_fopen is enabled) $html = file_get_contents('https://example.com');
Result
Open