Quick Search for:  in language:    
CGI,script,used,hide,actual,location,files,yo
   Code/Articles  |  Newest/Best  |  Community  |  Jobs  |  Other  |  Goto  | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 74,273. lines
 Jobs: 24. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for Perl.
Click here to see a screenshot of this code!CGIScripter
By David Simpson on 11/24

(Screen Shot)

Calender
By Jeff Mills on 11/20


quikpoll
By Jeff Mills on 11/20


Encrypt Password
By Jeff Mills on 11/20


Rock, Paper, Scissors w/ GUI
By Kurt Rudolph on 11/19


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Resume download and mask file location script

Print
Email
 
VB icon
Submitted on: 9/4/2003 10:06:54 PM
By: TiCaL 
Level: Intermediate
User Rating: By 3 Users
Compatibility:5.0 (all versions), Active Perl specific, 4.0 (all versions)

Users have accessed this code 1665 times.
 

 
     This CGI script can be used to hide the actual location of files on your website by masking. All files can be downloaded through this script so you can keep track of who is downloading what or simply restricting access. This script also fully supports resumed downloads (GetRight, Download Accelerator etc). Please vote if you find this script useful.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    =**************************************
    = Name: Resume download and mask file lo
    =     cation script
    = Description:This CGI script can be use
    =     d to hide the actual location of files o
    =     n your website by masking. All files can
    =     be downloaded through this script so you
    =     can keep track of who is downloading wha
    =     t or simply restricting access. This scr
    =     ipt also fully supports resumed download
    =     s (GetRight, Download Accelerator etc). 
    =     Please vote if you find this script usef
    =     ul.
    = By: TiCaL
    =
    = Inputs:The only parameter this Perl sc
    =     ript accepts is 'file'. Eg. download.pl?
    =     file=somethin.zip
    =
    =This code is copyrighted and has    = limited warranties.Please see http://w
    =     ww.Planet-Source-Code.com/vb/scripts/Sho
    =     wCode.asp?txtCodeId=532&lngWId;=6    =for details.    =**************************************
    
    #!/usr/bin/perl
    # Change the line above to suit your system
    # Disable buffered output
    $|++;
    # Attachment fix for Internet Explorer 5.5
    $browser = $ENV{'HTTP_USER_AGENT'};
    $isIE55 = ($browser =~ m/MSIE 5.5/g);
    $disp = "attachment;";
    if($isIE55 != 0)
    {
    	$disp = "";
    }
    # Get the filename parameter
    use CGI;
    $query = new CGI;
    $filename = $query->param('file');
    # Check if file parameter exists
    if(!($filename))
    {
    	# Redirect user to an error page
    	print "Location: 404.html\n\n";
    	# Exit script
    	exit 0;
    }
    # this is the directory where you hide your files
    $hidden_location = "myhiddenfolderpath";
    # remove all backreferences from filename
    # this is to stop hackers passing ../../../ type paths with the filename
    $filename =~ s/.*[\:\/\\](.*)/$1/;
    # check if file exists and if not show 404
    if(!(-e "$hidden_location/$filename"))
    {
    	# Redirect user to an error page
    	print "Location: 404.html\n\n";
    	# Exit script
    	exit 0;
    }
    # get the HTTP_RANGE enviroment variable is the client wishes to resume file download
    my $http_range = $ENV{HTTP_RANGE};
    my $range_start = 0;
    my $filesize = 0;
    # get the size of the file in bytes
    $filesize = -s "$hidden_location/$filename";
    # create default Content-Length and Content-Range header lines
    my $cl = "Content-Length: $filesize\nContent-Range: bytes 0-".($filesize-1)."/$filesize\n";
    # if the HTTP_RANGE variable exists
    if ($http_range) 
    {
    	# we extract try to extract the file offset --> $1
    	if ($http_range =~ m/^bytes=(\d*)-(\d*)$/) 
    	{
    		# if we don't find anything we default to offset 0
    		$range_start = $1 || 0;
    		$range_end= $2 || $filesize;
    		# if the requested range is larger than the file itself we have a bad request
    		if ($range_end > $filesize) 
    		{
    			# so we set our variables to defaults
    			$range_start = 0;
    			$cl = "Content-Length: $filesize\nContent-Range: bytes 0-".($filesize-1)."/$filesize\n";
    		}
    		# we have succesfully extracted the byte range requested so we recreate the header with proper info
    		$cl = "Content-Range: bytes $range_start-".($filesize-1)."/$filesize\n";
    		$cl .= "Content-Length: ".($filesize-$range_start)."\n";		
    	} 
    	else 
    	{
    		# if we are here it means that the HTTP_RANGE variable was malformed so we default our variables
    		$range_start = 0;
    		$cl = "Content-Length: $filesize\nContent-Range: bytes 0-".($filesize-1)."/$filesize\n";
    	}	
    }
    # Now we print the header
    # First tell client that we can resume downloads
    print "Accept-ranges: bytes\n";
    # Then print our Content-Length and Content-Range 
    print "$cl"; 
    # Now we force the browser to pop-up the save as dialogue
    print "Content-Disposition:$disp filename=$filename\n"; 
    print "Content-Type:application/x-download\n\n"; 
    # variable which will contain the partial file data when someone resumes
    my $filebuf;
    # Lets open the file so we can send it to the client
    open(DLFILE, "<$hidden_location/$filename") || die "Unable to open $filename because $!.\n";
    # Change to binary mode if operating system is Windows
    binmode(DLFILE) if $^O eq 'MSWin32';
    # If we are starting from 0 (not resuming) copy the file contents into fileholder array
    if($range_start == 0)
    {
    	@fileholder = <DLFILE>; 
    }
    else
    {
    	# We need to read the file data starting from requested offset
    	seek(DLFILE, $range_start, 0);
    	read(DLFILE, $filebuf, $filesize-$range_start); 
    }
    # Close the file
    close (DLFILE); 
    # Now we send the file to the client
    if($range_start == 0)
    {
    	# If we are not resuming
    	print @fileholder;
    }
    else
    {
    	# If we are resuming
    	print $filebuf;
    }
    # Exit script
    exit 0;

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
9/4/2003 10:56:09 PM:TiCaL
Please vote if you find this script 
useful and post comments. Thanks.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/5/2003 9:26:33 AM:
this is a great demonstration of
this 
protocol!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.