How can the header() function in PHP be used to control the content type of files being accessed?

To control the content type of files being accessed in PHP, the header() function can be used to set the Content-Type header before outputting any content. This ensures that the browser interprets the file correctly based on the specified content type.

// Set the content type to image/jpeg
header('Content-Type: image/jpeg');

// Output the image file
readfile('image.jpg');