|
|
#1 (permalink) |
|
Registered User
Fresh Surpasser
Joined in May 2004
10 posts
Gave thanks: 0
Thanked 0 times
|
Hello all,
I don't know if this is an html coding question or a java question. What I want to do is, when someone clicks on a link, I want that link to automatically download the file it's pointing to, rather than just open it. I would really like the simplest solution, if there is one. Is there a tag that I can add to the "a href"? Thanks in advance for any help! -T :-) |
|
|
|
|
|
#2 (permalink) |
|
after g, before i
Resident.
Joined in Jul 2004
Lives in N,BC,CA
8,092 posts
Gave thanks: 48
Thanked 131 times
|
Well, I think it depends on the type of file it is. Some files will just open automatically due to browser settings and certain software installed, while other times it will ask which to do.
Then again, I think it is possible because many sites I visited in the past had a prompt to download a JPG image. Anyone thing they could help them? |
|
|
|
|
|
#3 (permalink) |
|
Surpass Fan
Super #1
Joined in Aug 2004
Hosted on SH58
1,688 posts
Gave thanks: 6
Thanked 7 times
|
It's not HTML. I personally do it with PHP since I'm a PHP nut. Here, I'll walk you through it. We'll be using the file "test.mp3" which is located in: www.yourdomain.tld/downloads/test.mp3
1) Create your HTML link to download the file: Code:
<a href="download.php?file=test.mp3" title="Download File">Download File</a> PHP Code:
Note that I think it can be 'hacked' by using ../ which will point the script to the parent directory. A simple way to fix this is to add in a line which reads like this: PHP Code:
__________________
- Evan Charlton | [site] | Server - SH58 |
|
|
|
|
|
#4 (permalink) |
|
Registered User
Comfy Contributor
Joined in Dec 2004
Lives in Fairfax, VA, USA
Hosted on sh57
247 posts
Gave thanks: 0
Thanked 0 times
|
That script has several problems:
"application/force-download" is ugly and unnecessary. The "Content-disposition:" line is what forces a download. readfile(), at least on some systems, is implemented by reading the entire file into memory. This is not acceptable for large files. I wrote a similar script in reply to a question here about preventing streaming with Winamp. tcjahn, if you tell me what types of files (extensions) you want to use this for, I can make a modified script for you. Unfortunately it isn't as simple as adding an attribute to <a href=...>
__________________
Ben the Benly Benis: the greatest webcomic in existence. (on sh57) |
|
|
|
|
|
#5 (permalink) |
|
Registered User
Fresh Surpasser
Joined in May 2004
10 posts
Gave thanks: 0
Thanked 0 times
|
Thanks for your help, guys.
I am actually using a system that has specific folders already in place that I cannot change or add. If I'm to include a file of some kind, it must go into a "scripts" folder (../scripts/). In that folder exists files with extensions .js .asp .css .inc Also, my document (the one I'm trying to have download) must go into another directory: (../applications/documentlibrary/documents/) I realize that could be an added monkeywrench. Can the file be in a separate directory or does it have to be in the root? No matter, though, of what I have to deal with organizational-wise. I can figure out where to put things once I have the *basic idea.* I'd still like to know how to do this, even if it's outside of my current situation. I have a unique problem, and if it's way out there for an intermediate web designer, I understand. Thanks again! -T :-) |
|
|
|
|
|
#6 (permalink) |
|
Registered User
Comfy Contributor
Joined in Dec 2004
Lives in Fairfax, VA, USA
Hosted on sh57
247 posts
Gave thanks: 0
Thanked 0 times
|
What type of files do you want links to download?
.txt, .mp3, .exe, .hat, what?
__________________
Ben the Benly Benis: the greatest webcomic in existence. (on sh57) |
|
|
|
|
|
#7 (permalink) |
|
Registered User
Fresh Surpasser
Joined in May 2004
10 posts
Gave thanks: 0
Thanked 0 times
|
I just want to have a .html file download.
The situation is that I need to have it download, because it is customizable in Microsoft Word. The site works on an internal server. As it stands now, people are opening the file directly in the browser, editing it and then saving it to the same server, which overwrites my old file. I don't want that to happen. Thanks again, and thanks for your patience!! -T :-) |
|
|
|
|
|
#8 (permalink) |
|
Surpass Fan
Super #1
Joined in Aug 2004
Hosted on SH58
1,688 posts
Gave thanks: 6
Thanked 7 times
|
Yes, you can use that script with files in other directories. Just change the following line:
PHP Code:
__________________
- Evan Charlton | [site] | Server - SH58 |
|
|
|
|
|
#9 (permalink) |
|
Registered User
Comfy Contributor
Joined in Dec 2004
Lives in Fairfax, VA, USA
Hosted on sh57
247 posts
Gave thanks: 0
Thanked 0 times
|
Change
header( 'Content-Type: application/force-download' ); to header( 'Content-Type: text/html' ); in the above script. Also, it is very important you check for exploit attempts: if(strpos($_GET['file'], '/') || strpos($_GET['file'], ':') || strpos($_GET['file'], '[at]')) { header("HTTP/1.0 403 Forbidden"); die("That file doesn't exist here."); } Putting it all together, you'd have something like: Code:
<?php
if( isset( $_GET['file'] ) && !empty( $_GET['file'] ) ){
if(strpos($_GET['file'], '/') || strpos($_GET['file'], ':') || strpos($_GET['file'], '[at]'))
{
// Attempted exploit?
header("HTTP/1.0 403 Forbidden");
die("That file doesn't exist here.");
}
$file = $_SERVER['DOCUMENT_ROOT'].'../applications/documentlibrary/documents/'.$_GET['file'];
header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/force-download' );
header( 'Content-Length: '.filesize( $file ) );
header( 'Content-Disposition: attachment; filename='.urlencode( basename( $file ) ) );
readfile( $file );
}
?>
Post back if you have any problems with the code.
__________________
Ben the Benly Benis: the greatest webcomic in existence. (on sh57) |
|
|
|