Month: March 2025

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.

WordPress reserved terms you should never use

While developing WordPress theme or plugin we use different WordPress keywords or terms, additionally while handling forms fields and custom post types we have to consider what names we can use or not. The restricted names are called reserved terms.

WordPress itself reserves few keywords or terms which we can not use in our coding. Using these reserved or restricted terms just break the functionality what we intend
to get.

You should avoid using these reserved terms in the following scenarios:

  1. Passing a term through a $_GET or $_POST array.
  2. Registering a taxonomy or post type slug
  3. Handling query variables

WordPress Codex URL: https://codex.wordpress.org/Reserved_Terms

© 2025 Wavesdream Blog

Theme by Anders NorénUp ↑