How can the Content-Type header affect the "Save As" behavior in different browsers when downloading files using PHP?
The Content-Type header informs the browser about the type of content being served. When downloading files using PHP, setting the Content-Type header correctly is crucial for the browser to interpret the file type correctly and trigger the appropriate "Save As" behavior. If the Content-Type is not set or incorrect, the browser may not prompt the user to download the file or may not recognize the file type, leading to unexpected behavior.
<?php
// Set the Content-Type header based on the file type
header('Content-Type: application/pdf');
// Set the Content-Disposition header to force download with a specific filename
header('Content-Disposition: attachment; filename="example.pdf"');
// Output the file content
readfile('path/to/example.pdf');