What are the main functions used for enabling file downloads in PHP?
To enable file downloads in PHP, you can use the `header()` function to set the appropriate content type and headers for the file download. You also need to use `readfile()` or `file_get_contents()` to read the file contents and output them to the browser. Additionally, you can use `exit()` to prevent any further output from being sent to the browser after the file download.
$file = 'path/to/file.pdf';
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();
Related Questions
- What is the significance of using if-else statements in PHP scripts, especially when dealing with form submissions?
- Are there any recommended resources or tutorials for beginners to learn PHP basics and form validation?
- How can PHP be used to count the number of words in an array and compare it to a user input?