What function in PHP can be used to display a "Save As" option when clicking on files instead of opening images?
To display a "Save As" option when clicking on files instead of opening images in PHP, you can use the header() function to set the Content-Disposition to "attachment". This will prompt the browser to download the file instead of displaying it in the browser window.
<?php
$file = 'path/to/your/image.jpg';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>
Keywords
Related Questions
- How can PHP developers ensure that the URL paths they construct are valid and accessible?
- How can PHP developers optimize their code to display multiple results from a database query, as demonstrated in the forum thread?
- How can the code be refactored to improve performance and reduce memory consumption when working with image processing in PHP?