How can the header function be used to address the problem of opening .jpg files?

The header function can be used to address the problem of opening .jpg files by specifying the content type as image/jpeg before outputting the file. This ensures that the browser interprets the file correctly as a JPEG image.

<?php
$file = 'example.jpg';

header('Content-Type: image/jpeg');
readfile($file);
?>