To download a file from a server using PHP, you can use the following steps:
download.php
) and start the PHP code block.readfile()
function.Here is an example of the PHP code:
<?php
// Get the file path
$file = '/path/to/file.pdf';
// Check if the file exists
if (file_exists($file)) {
// Set headers to force file download
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;
} else {
// File not found
die('Error: File not found.');
}
?>
Make sure to adjust the $file
variable with the actual file path on your server.