TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (2) Thread Tools Search this Thread Display Modes
Old 09-19-2007, 02:52 PM   2 links from elsewhere to this Post. Click to view. #1 (permalink)
The Contributor
 
mortisimus's Avatar
 
Join Date: Sep 2007
Location: London, UK
Posts: 47
Thanks: 4
mortisimus is on a distinguished road
Default Forced Download

This script will get the file name from a url, it will assume that all files to be downloaded are on the same level as the download file, e.g:

Where to place the download file:
downloads/
download.php
[all other files]


File Linkage:
If you want to link to the file, use this sort of html code:
Code:
<a href="download.php?file=foo.html">Download</a>
In the example above the folder format would be:
/
file-linking-to-download.html
/downloads/
download.php
foo.html
Attached Files
File Type: zip download.zip (521 Bytes, 150 views)
mortisimus is offline  
Reply With Quote
The Following 3 Users Say Thank You to mortisimus For This Useful Post:
Andrew (01-07-2008), reborn (12-20-2007), Wildhoney (01-07-2008)
Old 09-19-2007, 03:01 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Thanks a lot for being kind enough to release this script for free! It'll come in use for many people, I'm sure.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-20-2007, 10:21 PM   #3 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 7
Thanks: 3
reborn is on a distinguished road
Default

Thanks. Will need this in near future.
reborn is offline  
Reply With Quote
Old 12-25-2007, 06:33 PM   #4 (permalink)
The Contributor
 
mortisimus's Avatar
 
Join Date: Sep 2007
Location: London, UK
Posts: 47
Thanks: 4
mortisimus is on a distinguished road
Default

no probs...
mortisimus is offline  
Reply With Quote
Old 12-25-2007, 08:23 PM   #5 (permalink)
The Acquainted
 
sjaq's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 113
Thanks: 11
sjaq is on a distinguished road
Bug

This script is really insecure, you can just say ?file=../../../../etc/passwd or something like that, for it to be safe you need to replace this line:
PHP Code:
$file $_GET['file']; 
with:
PHP Code:
$file str_replace('/'$_GET['file']); 
And why do you pass the same argument twice into the _Download function?
PHP Code:
_Download($file$file); 
sjaq is offline  
Reply With Quote
Old 12-26-2007, 05:14 PM   #6 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 7
Thanks: 3
reborn is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
And why do you pass the same argument twice into the _Download function?
PHP Code:
_Download($file$file); 
The first argument is the files location and filename and the second argument is the filename, wich is the same if the file and the script is in the same location.
reborn is offline  
Reply With Quote
Old 12-26-2007, 06:02 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

php Code:
function _Download($file, $file)
{
    echo 'File 1: ' . $file . "\r\n";
    echo 'File 2: ' . $file;
}

echo _Download('Test1', 'Test2');

Result:

Quote:
File 1: Test2
File 2: Test2
I think that's what sjaq is trying to point out. Both $file variables, because they're named the same, will have the result of the last argument set to $file. Which in my case was Test2.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-26-2007, 07:09 PM   #8 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 7
Thanks: 3
reborn is on a distinguished road
Default

The _Download() function is declared with the first argument $f_location and the second argument $f_name (if you have read the source).
reborn is offline  
Reply With Quote
Old 01-06-2008, 11:59 PM   #9 (permalink)
The Wanderer
 
Edwin's Avatar
 
Join Date: Jan 2008
Posts: 10
Thanks: 1
Edwin is on a distinguished road
Default

how can i qet this script to work?
Edwin is offline  
Reply With Quote
Old 01-07-2008, 12:41 AM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

What you having problems with exactly, Edwin? I'm guessing it's not a technical issue, but rather a general how to get it working?

Place your file (myFile.zip) in the downloads directory, and then link to the file like so:

html4strict Code:
<a href="download.php?file=downloads/myFile.zip">Download</a>
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-07-2008, 01:58 AM   #11 (permalink)
The Acquainted
 
Join Date: Sep 2007
Location: Arizona
Posts: 114
Thanks: 10
Andrew is on a distinguished road
Default

I was actually saying to myself the other day, I needed to find a script like this. Thanks a lot! I'll have to modify it a bit of course to fit my needs, but I never knew what headers to use. Thanks!
Send a message via AIM to Andrew Send a message via MSN to Andrew
Andrew is offline  
Reply With Quote
Old 01-07-2008, 03:03 AM   #12 (permalink)
The Wanderer
 
Edwin's Avatar
 
Join Date: Jan 2008
Posts: 10
Thanks: 1
Edwin is on a distinguished road
Default

thanx wildhoney
Edwin is offline  
Reply With Quote
Old 01-07-2008, 03:18 AM   #13 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Pleasure Edwin ! Are you much of a programmer yourself? Or planning to at least get into it?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-07-2008, 03:23 AM   #14 (permalink)
The Wanderer
 
Edwin's Avatar
 
Join Date: Jan 2008
Posts: 10
Thanks: 1
Edwin is on a distinguished road
Default

no im planninq to qet into it i know some php but not much.
Edwin is offline  
Reply With Quote
Old 01-17-2008, 03:57 AM   #15 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

It is a shame Would have been nice to have seen you round here more often! Nonetheless, best of luck with the project you're using this script for. Take it easy.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 01-17-2008, 11:54 AM   #16 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

i still dont get why you have 2 parameters in _Download, if both args take the same value (i.e. $_GET['file'] ) then whats the point, as far as i can see nothing changes the values of the parameters within the function so wouldnt this do the same:

PHP Code:
function _Download($file)
{
    
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    
header('Content-Description: File Transfer');
    
header('Content-Type: application/octet-stream');
    
header('Content-Length: ' filesize($file));
    
header('Content-Disposition: attachment; filename=' basename($file));
    
readfile($file);

looks like a waste of memory to me, note: i havnt tested it im at work and i cba.
and yes sjaq is correct, you will need some form of filtering for $_GET too otherwise anything could be inputted.

Other than that, good job.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 01-17-2008, 12:38 PM   #17 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

You are right, sketch. There is absolutely no need for the 2 functions because they'll go through as the same value anyway, and so if you wanted to clone the variable from within the function, you could do. And so the second argument is utterly pointless.

php Code:
function _Download($file)
{
    $file2 = $file;
    /* ... */
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/script-giveaway/1169-forced-download.html
Posted By For Type Date
header failure - SitePoint Forums This thread Refback 01-09-2008 12:39 PM
header failure - SitePoint Forums This thread Refback 01-09-2008 07:32 AM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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


All times are GMT. The time now is 03:56 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design