Jump to content

automatic download link?


andr923

Recommended Posts

I'm a musician.  On my website I want to provide people with files they can download such as images/wallpapers(jpg), sheet music (pdf) and music (mp3).  All of these files would typically open in a browser window if I linked to them directly.  I was wondering if anybody had a suggestion so that I could code a link that rather than open that file in the browser (like it would usually do), it would automatically start the download of that file and never even leave the current page.

 

Is this answer to zip the files that are to be downloaded or is there a more elegant way?

 

Thanks!

Link to comment
Share on other sites

the simplest thing to do is just tell people to right-click on the link and select "Save Target As..." to save the file to their computer without opening the file in their web browser.

 

your other option is a bit more complex.  have the link point to a php page that contains code that reads your actual file and then forces the user to download it...

something basicially like:

 

<?php
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename='filename_User_Will_See.pdf';")
readfile("/path/to/original/file.pdf")
?>

 

the key part above is "Content-Disposition: attachment" -- thats the part that SHOULD force the file to download.  May not always work in all browsers though.

 

PS, this is also a great method for hiding the actual location of your original files -- perhaps if you want to only make them available for logged in members

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

can I just place the code anywhere in the page? or does it have to be part of the header?  Will the rest of the page display normally if the code you gave me is in the header?  I guess I'm confused as to how exactly to implement it.

 

Thanks for all your help already.  and yes I did try and look up the different mime-types but wasn't satisfied as the one you pointed to didn't come up in my search.  thanks for the link!

Link to comment
Share on other sites

php header()'s must be called before any other information is sent to the browser...

 

in the example i gave you in my first response, those three are the only lines that need to be in the entire page.  After you call "readfile()" it will output the contents of that file to the browser.  Anything else you have on the page running your script will just mess up the output.

 

they should be in a file call, something like: "download.php"

<?php //download.php

$filepath = $_GET['file'];
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename='filename_User_Will_See.pdf';");
readfile($filepath);
?>

 

then on your originating page, you just link to it like you would link to any other file:

<a href="download.php?file=/path/to/original/file.pdf">download file</a>

 

thats the gist of it.

you'll probably want to do some other modification to the code and methodology.

for starters, instead of my example of including the original path as a variable in the link, pass an "ID number" then on the download.php page, use that ID to query a table of all your files.  The location to the file would be listed in said table.

If you have multiple types of files w/ differnt mime-types, this table could also store that information as well so that the download.php file can figure out which type of content-type to set in the header() field.

 

good luck.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.