icon Get the most out of Surmunity, read our tips here! Need an interesting blog to read? You've got to read the Surpass Blog! | Welcome! Please register to access all of our features.
Old February 23rd, 2005, 1:52 PM   #1 (permalink)
Registered User
Fresh Surpasser
 
Joined in May 2004
10 posts
Gave thanks: 0
Thanked 0 times
Question html code to download a file

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 :-)
tcjahn is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 23rd, 2005, 2:02 PM   #2 (permalink)
H
after g, before i
Resident.
 
H's Avatar
 
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?
H is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 23rd, 2005, 2:33 PM   #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>
2) Create your download.php and place the following code within it:
PHP Code:
<?php
    
if( isset( $_GET['file'] ) && !empty( $_GET['file'] ) ){
        
$file $_SERVER['DOCUMENT_ROOT'].'/downloads/'.$_GET['file'];
        
header'Content-Description: File Transfer' );
        
header'Content-Type: application/force-download' );
        
header'Content-Length: '.filesize$file ) );
        
header'Content-Disposition: attachment; filename='.urlencodebasename$file ) ) );
        
readfile$file );
    }
?>
Now, all you need to do if you want to add new files is upload them into /downloads/ and link to them with the URL of /download.php?file=yourfilename

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:
        $file preg_replace'/\.\.?\//'''$file ); 
__________________
- Evan Charlton | [site] | Server - SH58
Kickersny.com is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 23rd, 2005, 3:47 PM   #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)
graue is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 23rd, 2005, 4:15 PM   #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 :-)
tcjahn is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 23rd, 2005, 4:24 PM   #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)
graue is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 23rd, 2005, 5:24 PM   #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 :-)
tcjahn is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 24th, 2005, 12:38 AM   #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:
$file $_SERVER['DOCUMENT_ROOT'].'/downloads/'.$_GET['file']; 
to whatever your directory may be.
__________________
- Evan Charlton | [site] | Server - SH58
Kickersny.com is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old February 24th, 2005, 1:00 AM   #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 );
    }
?>
Replace [at] with Shift-2 in the above, and you should be ready to go. Save the above code in scripts/getfile.php and link to "scripts/getfile.php?file=whatever.html".

Post back if you have any problems with the code.
__________________
Ben the Benly Benis: the greatest webcomic in existence. (on sh57)
graue is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


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

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may 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