Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-09-2006, 04:19 PM   #1 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Making downloads private

I'm wondering if it's possible to make downloads private so that you have to be registered in order to download a file. Anyone?
__________________
Current Project
Redline is offline   Reply With Quote
Old 03-09-2006, 04:32 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,489
sde is on a distinguished road
here's a function someone posted on php.net a while back. just do your user authentication first before you call the download function.
PHP Code:
function dl_file($file){

   
//First, see if the file exists
   
if (!is_file($file)) { die("<b>404 File not found!</b>"); }

   
//Gather relevent info about file
   
$len filesize($file);
   
$filename basename($file);
   
$file_extension strtolower(substr(strrchr($filename,"."),1));

   
//This will set the Content-Type to the appropriate setting for the file
   
switch( $file_extension ) {
         case 
"pdf"$ctype="application/pdf"; break;
     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;
     case 
"mp3"$ctype="audio/mpeg"; break;
     case 
"wav"$ctype="audio/x-wav"; break;
     case 
"mpeg":
     case 
"mpg":
     case 
"mpe"$ctype="video/mpeg"; break;
     case 
"mov"$ctype="video/quicktime"; break;
     case 
"avi"$ctype="video/x-msvideo"; break;

     
//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
     
case "php":
     case 
"htm":
     case 
"html":
     case 
"txt": die("<b>Cannot be used for "$file_extension ." files!</b>"); break;

     default: 
$ctype="application/force-download";
   }

   
//Begin writing headers
   
header("Pragma: public");
   
header("Expires: 0");
   
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   
header("Cache-Control: public");
   
header("Content-Description: File Transfer");
  
   
//Use the switch-generated Content-Type
   
header("Content-Type: $ctype");

   
//Force the download
   
$header="Content-Disposition: attachment; filename=".$filename.";";
   
header($header );
   
header("Content-Transfer-Encoding: binary");
   
header("Content-Length: ".$len);
   @
readfile($file);
   exit;

you may have to tweak it a bit.
sde is offline   Reply With Quote
Old 03-09-2006, 04:43 PM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Alternately, provided you have this sort of access, you can use Apache to control which users can access what content - I think it's called mod_auth. Sde or someone will correct me if I'm wrong. I like the PHP way of doing it better myself, though.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help making a login script more secure superbeastie PHP 8 08-29-2005 07:48 AM
2D graphic repaint logic ralphtan Java 1 08-13-2004 03:49 AM
Napster sells 5 million downloads redhead Code Newbie News 0 02-26-2004 01:31 AM
Making the Move To Linux redhead Code Newbie News 0 07-04-2003 12:32 AM


All times are GMT -8. The time now is 10:31 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting