How can PHP be used to control the download location of a file on a website?
When serving files on a website using PHP, you can control the download location by setting the appropriate headers in the response. By specifying the "Content-Disposition" header with a value of "attachment; filename=yourfilename.extension", you can prompt the browser to download the file instead of displaying it in the browser window. This allows you to specify the default download location for the user.
<?php
// Set the file path
$file = 'path/to/yourfile.pdf';
// Set the headers
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="yourfile.pdf"');
// Output the file
readfile($file);
?>
Related Questions
- What is the function mysql_insert_id() in PHP and how can it be used to retrieve the ID of a newly inserted database entry?
- How can the size of a MySQL query be determined in PHP?
- What measures can be taken to restrict user access to specific directories within a PHP script to prevent unauthorized access?