Jump to content

Secure Download


hedgefighter

Recommended Posts

Make the download through a php file.

The file will check if the user is logged in. If he's not, redirect him to some login page or something. Now, that file is going to receive a url parameter (GET), will check if the file exists in the downloads folder (I will explain this soon) and if so force download it.

The downloads folder will contain all of the files + a htaccess file that contains the line:

Deny from all

 

Here's how the script should look (sorta):

<?php

$base_dir = "downloads/";

//Change this of course...
if(user is not logged)
{
header("Location: login.php");
exit;
}


if(!isset($_GET['file']) || !is_file($base_dir.$_GET['file']) )
die("File not found!");


//file was found & user is logged- force download it!
$filename = $base_dir.$_GET['file'];

if(ini_get('zlib.output_compression'))
  ini_set('zlib.output_compression', 'Off');

$extension = strtolower(substr(strrchr($filename,"."),1));
switch($extension)
{
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "doc": $ctype="application/msword"; break;
  case "xls": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  default: $ctype="application/force-download";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
@readfile($filename);
exit();

?>

 

All this will assure only logged users will be able to download the files.

 

 

Orio.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.