Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

download mp3 instead of playing in browser by default?

I have a site where users can stream my music from a flash player or download the individual songs (as mp3s). Right now if you click on the download links, they just play in the browser. Can I make it so the download box will pop up by default without either zipping the files making the user rt. click?

I have my index.php page with the links looking like this (pulling file names dynamically from mySQL:

<table>
.
.
.
<td>
    <?php include 'Media/' . $row['type'] . '/' . $row['folder_name'] . '/download.php' ?>
</td>

and then download.php has this:

<div class="downloadCell">
    <h3>Downloads:</h3>
    <ul>
        <li>
          <a href="auto_download.php?path=Media/<?php echo $row['type'] . '/' . $row['folder_name'] ?>/Chemtengure.mp3">Chemtengure</a>
        </li>
    </ul>
</div>

and I put the auto_download.php in the same directory as index.php:

auto_download.php:

$path = $_GET['path'];
header('Content-Disposition: attachment; filename=' . basename($path));
readfile($path);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will need to add this HTTP header to your response to make the browser force a download:

Content-Disposition: attachment; filename=your_filename.mp3

For example, if you're using PHP on the server side, you can create a script that sends the above header followed by the actual file contents:

$path = $_GET['path'];
header('Content-Disposition: attachment; filename=' . basename($path));
readfile($path);

If you call this script download.php, then linking to download.php?path=/path/to/your/file.mp3 will make the browser pop-up a download dialog for file.mp3.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...