Need to create Download Button php for website

  • fishdish
  • Born
  • Born
  • No Avatar
  • Joined: Jan 31, 2009
  • Posts: 1
  • Status: Offline

Post January 31st, 2009, 2:54 pm

Hello, I am a self taught web designer. I use Yahoo Sitebuilder. I have build a website for a small church that has the pastor's sermons online and available for download.

Up to this point I have simply given instructions in right-click and save. However, the church and myself want to make the site more professional looking.

Download buttons are one of those little things that just make a site look better and for the computer illiterate, less frightening.

I found an old post from back in 2007 of a php script for the very purpose I want. To be able to have the visitor click a Download button and be prompted to Open or Save.

However, I am missing something. Not putting the php file in the right place or something. I feel as if I am missing one tiny bit of assumed information. Could someone please help this frustrated designer? You would have my thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post January 31st, 2009, 2:54 pm

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8922
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post January 31st, 2009, 6:43 pm

You must change the content-type of what you are trying to give them. For example here is a small php script that would force the download of a file:

Code: [ Select ]
<?php

// force to download a file
$file = "http://www.yourdomain.com/downloads/".$_GET['file']."";

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");
@readfile($file);

?>
  1. <?php
  2. // force to download a file
  3. $file = "http://www.yourdomain.com/downloads/".$_GET['file']."";
  4. header("Pragma: public");
  5. header("Expires: 0");
  6. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  7. header("Content-Type: application/force-download");
  8. header( "Content-Disposition: attachment; filename=".basename($file));
  9. header( "Content-Description: File Transfer");
  10. @readfile($file);
  11. ?>


Also here is a link that shows a sample php script that might work better:

http://elouai.com/force-download.php

Hopefully that gets you on the right track.
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • Songcat
  • Born
  • Born
  • No Avatar
  • Joined: Oct 30, 2011
  • Posts: 3
  • Status: Offline

Post October 30th, 2011, 11:06 am

Hi
I'm a total PHP newbie. I would be so grateful if there were comments on the lines of this PHP script. Particularly for definitions and clarifications of the fields.
I'm trying to create a download for a file named myfile.epub. The mime-type is application/xhtml+xml.
My questions, with line numbers:
04 - explicitly explain 'file'. Does it mean the file to be downloaded, or the PHP file? (Sorry, I told you I was a newbie.)
06 - what does this do?
11 - what is .basename? what is $file? Can you give an example?
Moreover, what is the meaning of "you must change the content-type.." What is the exact definition of "content-type?"
Thanks so much for help.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8922
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post October 30th, 2011, 1:08 pm

For line #4 which says:

PHP Code: [ Select ]
$_GET['file']


That is basically just saying grab a GET variable. What that means is if your link in your browser says something like:

http://www.yourdomain.com/download.php?file=somefilename.mpg

Then download.php would be the script itself and everything after the question mark gets stored in the GET 'file' variable. Keep in mind you should actually sanitize any input in case someone tries to enter something that could cause problems for you. Look up PHP input sanitize and you should find many articles on it as its a huge topic. You don't even have to use the GET variable if you want to just hardcode some file in. That variable was basically supposed to allow you to use it for a variety of different file names.

For line #6:

Code: [ Select ]
Pragma: public


You could probably get by without it. It is related to cache control and seems to fix errors in IE8 and less where it sometimes you may get "Internet Explorer was not able to open this Internet site".

For the last line, #11:

PHP Code: [ Select ]
header( "Content-Disposition: attachment; filename=".basename($file));


The '.' before the basename in PHP means to append to the string. So in this case you will have the sring:

"Content-Disposition: attachment; filename="

and then appended to that is:

basename($file)

That PHP function called basename returns the trailing name component of path. In other words, given a string containing the path to a file or directory, this function will return the trailing name component. So in this case just the file name itself, not any path information. So instead of:

/home/path/to/your/filename.mpg

It would return:

filename.mpg
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • Songcat
  • Born
  • Born
  • No Avatar
  • Joined: Oct 30, 2011
  • Posts: 3
  • Status: Offline

Post October 30th, 2011, 5:17 pm

Oh Yeah! That was great.
Very cool. Thanks
  • Songcat
  • Born
  • Born
  • No Avatar
  • Joined: Oct 30, 2011
  • Posts: 3
  • Status: Offline

Post October 30th, 2011, 5:47 pm

Well, it works great on my pc with Google Chrome, but doesn't work on my Android phone. I get a download error indicating that the content is not supported. I guess the server doesn't have php support.
  • kevinnguyen
  • Born
  • Born
  • No Avatar
  • Joined: May 31, 2012
  • Posts: 1
  • Status: Offline

Post May 31st, 2012, 9:26 pm

Thank you, I’ve recently been searching for info about this topic for ages and yours is the greatest I’ve discovered till now. But, what about the conclusion? Are you sure about the source?
  • Zealous
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 15, 2011
  • Posts: 1194
  • Loc: Sydney
  • Status: Offline

Post May 31st, 2012, 10:40 pm

Songcat wrote:
Well, it works great on my pc with Google Chrome, but doesn't work on my Android phone. I get a download error indicating that the content is not supported. I guess the server doesn't have php support.


I guess your phone does not understand standard internet protocol, There may need different code for mobile use? Do some research

kevinnguyen wrote:
Thank you, I’ve recently been searching for info about this topic for ages and yours is the greatest I’ve discovered till now. But, what about the conclusion? Are you sure about the source?


That is Big Web your asking that too there is a reason he is the Big Web Master lololol! Are you sure you have tested it yet or learn how to apply it.
  • Ruttah
  • Newbie
  • Newbie
  • User avatar
  • Joined: May 29, 2012
  • Posts: 9
  • Status: Offline

Post June 10th, 2012, 8:50 pm

Used your code and added a little bit at the end from another code.. Someone was trying to help me get a download link to download mp3 and this worked :) Thanks





Code: [ Select ]
<?php
$file = "C:\xampp\htdocs\JosueWebsite\Mp3".$_GET['mp3']."";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=GucciWild.mp3".basename($mp3));
header( "Content-Description: File Transfer");
@readfile($file) OR die("<html><body OnLoad='javascript: alert('Unable to read file!');history.back();' bgcolor='#F0F0F0'>Unable to read file!</body></html>");
exit;

?>
  1. <?php
  2. $file = "C:\xampp\htdocs\JosueWebsite\Mp3".$_GET['mp3']."";
  3. header("Pragma: public");
  4. header("Expires: 0");
  5. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  6. header("Content-Type: application/force-download");
  7. header( "Content-Disposition: attachment; filename=GucciWild.mp3".basename($mp3));
  8. header( "Content-Description: File Transfer");
  9. @readfile($file) OR die("<html><body OnLoad='javascript: alert('Unable to read file!');history.back();' bgcolor='#F0F0F0'>Unable to read file!</body></html>");
  10. exit;
  11. ?>

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: Kurthead+1 and 156 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.