Below is a simple example of a PHP script that allows you to download file from a specific folder. You can link to this PHP file with the file name as a parameter.
Create a file named download.php and add the following code:
<?php // Specify the folder where your files are stored $folderPath = '/path/to/your/files/'; // Get the file name from the query parameter if (isset($_GET['file'])) { $fileName = basename($_GET['file']); $filePath = $folderPath . $fileName; // Check if the file exists if (file_exists($filePath)) { // Set headers for download header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $fileName . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filePath)); // Read the file and output it to the browser readfile($filePath); exit; } else { echo 'File not found.'; } } else { echo 'File parameter missing.'; } ?>
<a href="download.php?file=myfile.txt">Download myfile.txt</a>
Make sure to adjust the link and file names accordingly. Note that this is a basic example, and you may need to add additional security measures based on your specific requirements, such as checking user permissions and validating file types.
Comments
Post a Comment