0

I want to create a download button using HTML and PHP. Below is my HTML code -

<!DOCTYPE html>
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   </head>
   <body>
       <form action="download.php" method="post">
           <input type="submit" name="submit" value="Download File" />
       </form>
   </body>
</html>

Now I have created another file, called "download.php" and kept it in the same directory as my HTML file. Code of the PHP file is given below -

<?php
  header('Content-Type: application/download');
  header('Content-Disposition: attachment; filename="Sample.mp3"');
  header("Content-Length: " . filesize("Sample.mp3"));
  $fp = fopen("Sample.mp3", "r");
  fpassthru($fp);
  fclose($fp);
?>

Sample.mp3 is kept in same folder as HTML file. I got these codes from internet.

Now my problem is, when I click on download button, content of the PHP file get opened rather than downloading the file.

Can anyone please help me to fix this issue.

8
  • Stupid questions perhaps but is PHP actually activated on the server? Commented Jul 22, 2013 at 9:50
  • application/download should be audio/mpeg Commented Jul 22, 2013 at 9:51
  • Which webserver are you using? Commented Jul 22, 2013 at 9:51
  • Did you read this one yet? [stackoverflow.com/questions/15156711/… [1]: stackoverflow.com/questions/15156711/… Commented Jul 22, 2013 at 9:52
  • Not sure whether PHP is activated or not. I'll check and get back to you. Commented Jul 22, 2013 at 9:53

5 Answers 5

1

Now my problem is, when I click on download button, content of the PHP file get opened rather than downloading the file.

You need to run the script on:

  • A webserver
  • That supports PHP
  • That is configured to treat that file as PHP
Sign up to request clarification or add additional context in comments.

Comments

1

You probably not run your .php file in a web server.

  • Please install a personal web server to your computer like XAMPP or WAMP
  • Then put your files into the www folder
  • Open your browser
  • Type localhost/your_HTML_file

3 Comments

Dont try man, just delete your answer so the theoretical guys take all the points
Why should this work when the original code did not? What did you change and why?
@vlzvl, Quentin is right. We should have read the question till the end.
1

using PHP is more coding need ! If you are using html5 and only do this for download purpose ,you can use <a> link,that is easy and simple

<!DOCTYPE html>
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   </head>
   <body>
      <a href="Sample.mp3" download>Download </a>
   </body>
</html>

Comments

1

This worked for me :)

<a href="/path/to/downloadfile.csv">
    <button class="btn">
        <i class="fa fa-download"></i> Download
    </button>
</a>

Comments

-2

Try this in php file:

$file = 'Sample.mp3'
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;

2 Comments

Why should this work when the original code did not? What did you change and why?
@Quentin is right. We should have read the question till the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.