
Originally Posted by
anti-hero
I just wanna know whats the code so that when you click a link it turns into a download
There is no code, as such, that causes a dialogue box to query, "Open or Save to disk?"
When a browser makes a request for a resource, such as a HTML document or an image, the server (or cache) sends back that resource with a content type indicating what that resource is, and allowing the browser to decide how to handle it. When the browser encounters a content type that it doesn't recognise, or one that it hasn't been told to handle itself, it asks the user to download the data.
So, one way to get this effect is to make the server send an unrecognised content type, or one that browsers should not attempt to handle. The latter, and preferred, is application/octet-stream which implies (generic) binary data. For instance, you could state that all files that map to the directory,
  http://www.example.com/downloads/
should be served with application/octet-stream on an Apache server using:
Code:
<Location /downloads/>
ForceType application/octet-stream
</Location>
A second approach uses a commonly implemented extension called content disposition. This was originally meant for use with MIME-encoded messages, but was added (unofficially) to HTTP. It allows you to mark a resource as a separate, attached resource, and suggest a download name for it. It is a HTTP header like Content-Type (discussed above), and must also be sent by the server. You could do a similar thing as in the example above:
Code:
<Location /downloads/>
Header set Content-Disposition "attachment; filename=name"
</Location>
Unfortunately, some browsers (some versions of IE, in particular) don't react as they should. Using both of these methods in combination is the best approach, but it isn't guaranteed to work in all cases.
To use the directives above, you need to modify the server configuration, which might not be possible. An alternative is to remove the Location directives (leaving just Header and ForceType) and place them in a .htaccess file (that is, dot-htaccess) in the directory in question. Be aware that the behaviour here will be slightly different. The Location directive works by matching URLs, whereas a .htaccess file works like the Directory directive which operates on the filesystem of the server; where the files are actually stored. This isn't necessarily the same thing as URLs don't need to map directly to a filesystem location.
Mike
Bookmarks