How can PHP be used to offer downloads from a directory protected by .htaccess?

To offer downloads from a directory protected by .htaccess using PHP, you can create a PHP script that reads the files from the directory and sends them to the user. You can use PHP's header function to set the appropriate content type and headers for the download. Make sure that the .htaccess file allows access to the PHP script.

<?php
$dir = 'protected_directory/';
$file = $_GET['file'];

if (file_exists($dir . $file)) {
    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($dir . $file));
    readfile($dir . $file);
    exit;
} else {
    echo 'File not found.';
}
?>