Are there best practices for setting the Content-Type header in PHP to ensure proper video playback on different players?
When serving video files in PHP, it is important to set the Content-Type header correctly to ensure proper playback on different players. The Content-Type header specifies the type of data being sent, and for video files, it should be set to "video/mp4" for MP4 files, "video/webm" for WebM files, and "video/ogg" for Ogg files. This helps the browser and video player to interpret the content correctly and play the video without issues.
$videoFilePath = 'path/to/video.mp4';
$extension = pathinfo($videoFilePath, PATHINFO_EXTENSION);
switch ($extension) {
case 'mp4':
$contentType = 'video/mp4';
break;
case 'webm':
$contentType = 'video/webm';
break;
case 'ogg':
$contentType = 'video/ogg';
break;
default:
$contentType = 'video/mp4';
}
header('Content-Type: ' . $contentType);
readfile($videoFilePath);
Related Questions
- What potential issues can arise when passing variables using hidden fields in PHP forms?
- Are there any alternative methods, besides get_browser(), to accurately determine the browser and operating system using PHP?
- What are some best practices for ensuring timely display of output in PHP scripts with long processing times?