Monday, December 13, 2010

Php Script for File Downloder

This is a simple script to enable your visitors download your files. The script can
be used for protection from direct download, check user's authentication, write logs,
and such.

Features
--------
* Can download any files: zip, images, pdf, php, etc.
* Displays a 'You are downloading...' page before sending the file.
* Simple and easy to develop.


INSTALLATION
------------
1. Move the script to your site, eg. http://yourdomain.com/download.php
2. If your download files reside in another directory, change the line:

    $fileserver_path = dirname(__FILE__);

   to the directory where your files reside. maybe something like this:
  
    $fileserver_path = '/var/www/pub/downloads';

3. Refer your downloads to this script. eg:
   
    http://yourdomain.com/download.php?file=how_to_make_money_online.pdf

   

<?php
/*
 * use this script to enable your visitors to download
 * your files. 
 */

$fileserver_path = dirname(__FILE__);    // change this to the directory your files reside
$req_file          = basename($_GET['file']);
$whoami             = basename(__FILE__);    // you are free to rename this file 

if (empty($req_file)) {
    print "Usage: $whoami?file=&lt;file_to_download&gt;";
    exit;
}

/* no web spamming */
if (!preg_match("/^[a-zA-Z0-9._-]+$/", $req_file, $matches)) {
    print "I can't do that. sorry.";
    exit;
}

/* download any file, but not this one */
if ($req_file == $whoami) {
    print "I can't do that. sorry.";
    exit;
}

/* check if file exists */
if (!file_exists("$fileserver_path/$req_file")) {
    print "File <strong>$req_file</strong> doesn't exist.";
    exit;
}

if (empty($_GET['send_file'])) {
    header("Refresh: 5; url=$whoami?file=$req_file&send_file=yes");
}
else {
    header('Content-Description: File Transfer');
    header('Content-Type: application/force-download');
    header('Content-Length: ' . filesize("$fileserver_path/$req_file"));
    header('Content-Disposition: attachment; filename=' . $req_file);
    readfile("$fileserver_path/$req_file");
    exit;
}
?>

<!-- Change the HTML page below for your convenient -->
<html>
<head>
<title>Simple File Server and Downloader Demo Page</title>
<!-- Maybe Google Analytics here? -->
</head>
<body>
<h2>Downloading <?=$req_file?>...</h2>
<p>Your download should begin shortly. If it doesn't start, 
   follow this <a href="<?=$req_file?>">link</a>.</p>
</body>
</html>

By PHP with No comments

0 comments:

Post a Comment

    • Popular
    • Categories
    • Archives