How can PHP be optimized to avoid exceeding memory limits when reading large video files for streaming?
When reading large video files for streaming in PHP, it's important to optimize memory usage to avoid exceeding limits. One way to achieve this is by using streaming techniques such as reading and outputting the file in chunks rather than loading the entire file into memory at once. This can be done using functions like `fopen`, `fread`, and `echo` to read and output the file in manageable portions.
$videoFilePath = 'path/to/video.mp4';
$handle = fopen($videoFilePath, 'rb');
while (!feof($handle)) {
echo fread($handle, 8192);
ob_flush();
flush();
}
fclose($handle);
Related Questions
- Are there any best practices for efficiently reading and processing data from a .txt file in PHP?
- How can the desired output of grouping and displaying data be achieved in PHP when querying a database like MSSQL?
- What are the potential performance issues when creating and importing data into multiple databases using PHP and SQL files?