How can PHP be used to create a download link for files on a server?
To create a download link for files on a server using PHP, you can use the header() function to set the appropriate content type and disposition for the file. You also need to specify the file path and name in the href attribute of the anchor tag.
<?php
$file = 'path/to/your/file.pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . basename($file));
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
} else {
echo 'File not found';
}
?>
Keywords
Related Questions
- How can the use of variable naming conventions impact code readability and maintenance in PHP scripts?
- What strategies can be implemented to improve the efficiency and readability of PHP code when managing shift schedules and employee assignments based on time of day?
- How can one efficiently display multiple posts in a PHP script without causing an infinite loop or displaying duplicate content?