PHP script to download file from specific folder

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.';
}
?>

Replace ‘/path/to/your/files/’ with the actual path to the folder where your files are stored. Now, you can link to this script by providing the file name as a parameter, like this:

<a href=”download.php?file=myfile.txt”>Download</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.