Need to create Download Button php for website

  • fishdish
  • Born
  • Born
  • fishdish
  • Posts: 1

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

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.
  • Songcat
  • Born
  • Born
  • Songcat
  • Posts: 3

Post 3+ Months Ago

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
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

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
  • Songcat
  • Born
  • Born
  • Songcat
  • Posts: 3

Post 3+ Months Ago

Oh Yeah! That was great.
Very cool. Thanks
  • Songcat
  • Born
  • Born
  • Songcat
  • Posts: 3

Post 3+ Months Ago

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
  • kevinnguyen
  • Posts: 1

Post 3+ Months Ago

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
  • Posts: 1240
  • Loc: Sydney

Post 3+ Months Ago

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
  • Posts: 9

Post 3+ Months Ago

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: No registered users and 64 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
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.