How can the header function in PHP affect the output of an image file?
When using the header function in PHP to send headers, it can affect the output of an image file by changing the content type header. This is important when serving image files from a PHP script, as the correct content type must be set to ensure the image is displayed correctly in the browser. To fix this issue, you can use the header function to set the content type to image/jpeg, image/png, or any other appropriate image format before outputting the image data.
<?php
// Set the content type header to image/jpeg
header('Content-Type: image/jpeg');
// Output the image file
readfile('image.jpg');
?>
Keywords
Related Questions
- How can placeholders be used to simplify the process of updating database credentials in PHP scripts?
- How can PHP scripts be triggered automatically in response to user input without reloading the entire form?
- What is the significance of the error message "Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given" in PHP and how can it be resolved?