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 functions like urlencode and http_build_query help in handling special characters in PHP URLs?
- What are some alternative approaches to implementing a CatClose feature in PHP, aside from the example provided in the forum thread?
- What security considerations should be taken into account when creating a browser-based PHP application with administrative rights for CD usage?