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');
Keywords
Related Questions
- How can the combination of htmlspecialchars and mysql_real_escape_string affect the output and storage of data in PHP applications?
- What are the best practices for generating input fields with dynamic names in PHP forms?
- What are the potential pitfalls of using imagecopyresampled versus imagecopyresized when dealing with transparent GIFs?