What are some best practices for optimizing memory usage when working with video files in PHP?

When working with video files in PHP, it's important to optimize memory usage to prevent performance issues. One way to achieve this is by streaming the video file instead of loading the entire file into memory at once. This can be done by reading and outputting chunks of the file data as it's being processed.

$videoFilePath = 'path/to/video.mp4';

$handle = fopen($videoFilePath, 'rb');
while (!feof($handle)) {
    echo fread($handle, 8192); // Output 8KB chunks of the file data
    ob_flush();
    flush();
}
fclose($handle);