How can you properly set the Content-Type header when serving images in PHP to ensure they are displayed correctly?
When serving images in PHP, it is important to set the Content-Type header correctly to ensure that the browser knows how to display the image. This can be done by using the header() function in PHP to set the Content-Type to the appropriate image type, such as "image/jpeg" for JPEG images or "image/png" for PNG images.
// Set the Content-Type header based on the image type
header('Content-Type: image/jpeg');
// Output the image data
readfile('path/to/image.jpg');
Keywords
Related Questions
- What potential pitfalls should PHP developers be aware of when dealing with session variables like uniqid and timestamp?
- What are the potential pitfalls of using if-elseif statements instead of switch-case statements when handling different page requests in PHP?
- Are there any potential security risks associated with using $_SERVER['HTTP_ACCEPT_LANGUAGE'] to retrieve the browser language?