What are some best practices for reading specific parts of a file in PHP?

When working with large files in PHP, it is important to read specific parts of the file efficiently to avoid memory issues. One way to achieve this is by using fseek() to move the file pointer to the desired position and fread() to read a specific number of bytes from that position.

$file = fopen('example.txt', 'r');
$position = 100; // desired position to start reading from
$length = 50; // number of bytes to read

fseek($file, $position);
$data = fread($file, $length);

fclose($file);

echo $data;