views:

18

answers:

1

Hi guys, 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);
+2  A: 

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.

casablanca
Can you elaborate a little bit? I'm afraid I don't quite understand how to code that. Does that GET just go up in the head section- or it's an include? Or -dang I'm still a noob. :D
Joel
The code You see here is everything You need. Just put it in a php file and point downloads to this file like `<a href="dl.php?path=/mp3s/blink128/fadog.mp3">get this song</a>`
naugtur
I'm not getting it to work-I think because I am adding some complexity (pulling download filename dynamically from mySQL. I have updated my question to share those details.
Joel
doh-I just didn't have php tags in auto_download.php Works great now. Thank you guys so much-you're awesome!
Joel