What is the purpose of using the header function with 'Content-type: image/jpeg' in PHP?
When using the header function with 'Content-type: image/jpeg' in PHP, the purpose is to specify the type of content being sent to the browser as an image file in JPEG format. This is important for ensuring that the browser interprets the data correctly and displays the image as intended. Without setting the correct content type, the browser may not render the image properly or may try to display it as plain text.
<?php
// Set the content type header to specify that the data being sent is an image in JPEG format
header('Content-type: image/jpeg');
// Output the image data
echo file_get_contents('image.jpg');
?>
Keywords
Related Questions
- What is the alternative method in PHP, besides explode(), to split a string into multiple variables based on a delimiter?
- What are some best practices for handling pagination in PHP to prevent displaying more results than available?
- What potential pitfalls should be considered when using preg_match_all in PHP to parse data with varying formats?