Example 1 — Read the First Bytes
Read the first nine bytes of a file.
<?php
$file = __DIR__ . '/fread_demo.txt';
file_put_contents($file, 'CodeToFun PHP');
$fp = fopen($file, 'r');
$data = fread($fp, 9);
fclose($fp);
echo "First 9 bytes: $data", PHP_EOL;
unlink($file); How It Works
- The file holds the text CodeToFun PHP.
- fopen() opens it for reading, and the pointer starts at the first byte.
- fread() takes the next nine bytes, which spell CodeToFun.
- fclose() closes the file and the text is printed.

