How to download a php script directly in your script page ?



How to download a php script directly in your script page ?

Here we create a PHP script, name it “download.php” and copy/paste the following code:

<?php
 
ignore_user_abort(true);
set_time_limit(0); // disable the time limit for this script
 
$path = "/absolute_path_to_your_files/"; // change the path to fit your websites document structure
 
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $_GET['download_file']); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$fullPath = $path.$dl_file;
 
if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    switch ($ext) {
        case "pdf":
        header("Content-type: application/pdf");
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a file download
        break;
        // add more headers for other content types here
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
        break;
    }
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
exit;

Create on your webpage links for each file which attacks in a password protected directory or in a directory above the website root directory.

In our example use the following download URL:

<a href="https://mydomain.com/download.php?download_file=some_file.pdf">PHP download file</a>


Related Searches to How to download a php script directly in your script page ?