Re: PHP5 - How Do I Let Users Download Music Files?
| From: | Daniel Schierbeck | Date: | Fri, 05 Sep 2003 10:45:11 +0000 |
| Subject: | Re: PHP5 - How Do I Let Users Download Music Files? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-195905@lists.php.net to get a copy of this message | ||
php-list@xenonsolutions.com wrote:
Hello everyone, I want to build a music site, all copyrights intact, and I want users to be able to download mp3 or realplayer files using a one-click link. When they click on a link they will simply be given a typical download window to save that music file. How do I go about doing that and how should my file structure work? What should the link include? Is it possible to just say, for example, http://somesite.com/music/something.mp3? If you want more control over the files you could have the mp3's in a private directory, then using PHP to force a download. There are some other threads about the Content-Type header, which is probably what you're looking forImagine that your file, 3416.mp3 is in /songs. 3416 is the song ID, so you can grab info such as artist, album and song name from a DB. Below is download.php, to download 3416.mp3, you should type download.php?file=3416 <?php if (!isset($_GET['file']) or !file_exists('songs/' . $_GET['file'] . '.mp3')) { die("No file selected"); } else { $file = $_GET['file']; } $db = mysql_connect(); // More DB stuff $artist = $row['artist']; // The artist name from the DB $album = $row['album']; // ... $song = $row['song']; // ... $filename = $artist . '-' . $album . '-' . $song . '.mp3'; header('Content-Type: application/force-download'); header('Content-Length: ' . filesize('songs/'.$file.'.mp3')); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Content-Transfer-Encoding: binary'); readfile('songs/' . $file . 'mp3'); exit(); ?>
Also, if I wanted them to listen to streamed music does PHP have any specialized functions I could use to achieve that?You should take a look at the Ming library and the mp3 stream function (http://www.php.net/manual/en/function.swfmovie.streammp3.php). It's a bit tricky though, but i can't think of anything else. -- Daniel Schierbeck