Jump to content

A download page?


Jragon

Recommended Posts

I found this to help you out

create a file called download.php and put this code in it

<?php
$path = $_SERVER['DOCUMENT_ROOT']."/"; // change the path to fit your websites document structure
$fullPath = $path.$_GET['download_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"); // add here more headers for diff. extensions
header("Content-type: application/php");
        header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
        break;
        default;
        header("Content-type: application/octet-stream");
        header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    }
    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;
?>

Be sure to look at the top of the script to make sure the path to your files is correct

 

then on the page you want to download files from ad links like this

<a href="download.php?download_file=file_to_download.php">Download Here</a>

 

Link to comment
https://forums.phpfreaks.com/topic/222572-a-download-page/#findComment-1151088
Share on other sites

There is no need to set the exact content-type, application octet-stream will work.

 

If you're allowing users to download PHP scripts then you must check the file requested is not one of your websites core PHP scripts for example your database credentials script.

 

Have a look at this example from here.

 

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/222572-a-download-page/#findComment-1151187
Share on other sites

The first part of the script points to the directory of downloadable files, as long as you only kept the files for download in that directory the core site php files would be safe

 

Ok so the path is set to document_root/downloads

 

I could just post:

 

../includes/dbconnect.php

Link to comment
https://forums.phpfreaks.com/topic/222572-a-download-page/#findComment-1151217
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.