How can PHP be used to prevent files from being opened in the browser and only allow them to be saved?

To prevent files from being opened in the browser and only allow them to be saved, you can set the appropriate headers in PHP to force the browser to download the file instead of displaying it. This can be achieved by sending the Content-Disposition header with the value 'attachment'. This will prompt the browser to save the file to the user's computer instead of displaying it in the browser.

<?php
$file = 'path/to/your/file.pdf';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
?>