andr923 Posted July 4, 2007 Share Posted July 4, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/58429-automatic-download-link/ Share on other sites More sharing options...
micah1701 Posted July 5, 2007 Share Posted July 5, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/58429-automatic-download-link/#findComment-290645 Share on other sites More sharing options...
gerkintrigg Posted July 18, 2007 Share Posted July 18, 2007 this is perfect for me!!!!! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/58429-automatic-download-link/#findComment-301257 Share on other sites More sharing options...
andr923 Posted August 1, 2007 Author Share Posted August 1, 2007 beatiful micah!! thanks. Yes I think I'll use the PHP code because I'll have some songs and sheet music that I will only want members of my mailing list to be able to download. Quote Link to comment https://forums.phpfreaks.com/topic/58429-automatic-download-link/#findComment-313226 Share on other sites More sharing options...
andr923 Posted August 9, 2007 Author Share Posted August 9, 2007 how should I change that code if the files to be downloaded are mp3 files? Quote Link to comment https://forums.phpfreaks.com/topic/58429-automatic-download-link/#findComment-319534 Share on other sites More sharing options...
micah1701 Posted August 9, 2007 Share Posted August 9, 2007 how should I change that code if the files to be downloaded are mp3 files? change: header("Content-Type: application/pdf"); to: header("Content-Type: audio/mpeg3"); see also: http://www.webmaster-toolkit.com/mime-types.shtml Quote Link to comment https://forums.phpfreaks.com/topic/58429-automatic-download-link/#findComment-319583 Share on other sites More sharing options...
andr923 Posted August 10, 2007 Author Share Posted August 10, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/58429-automatic-download-link/#findComment-320291 Share on other sites More sharing options...
micah1701 Posted August 10, 2007 Share Posted August 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/58429-automatic-download-link/#findComment-320377 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.