Jump to content

php download script for downloading mp3's from mysql database not working


phpphreak

Recommended Posts

Hi, this is my first post:) pretty sure i will be posting here in the future. Anyway i am having trouble with a php script which downloads a file from a mysql database.

 

This php script works for text files and should work for most files from what i understand. When downloading a text file, the whole file is downloaded from the server. When downloading an mp3 file, only 16kb are downloaded and the file does not play. When looking at the data in the database, it displays that the full file is there (correct amount of bytes, so there is nothing wrong with my upload php script). Does anyone have any suggestions as to what I can change to make this work?

 

<?php
if(isset($_GET['id'])) 
{
// if id is set then get the file with the id from database
$link=mysql_connect('localhost', 'root', '');
@mysql_select_db('filemgr') or die ("<p>Could not connect to mysql!</p>");

$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) =  mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type:$type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

mysql_close($link);
exit;
}
?>

Link to comment
Share on other sites

You are storing mp3 files as binary data (blob)? I've never tried and seen such an approach, but I'm guessing it's not the best. What if there are thousands of mp3s downloaded by hundreds of users? I doubt reading that much data from a database will perform well.

 

I would go for a normal approach, where you save the mp3 as a normal file and store in a database just the path. After that, you can use pretty much the same code as you have, but you'll need to use readfile instead of just echo()ing the content.

 

Finally, if you're serving big mp3 files, you'll have to split them in chunks or better, use an apache module (if you're using apache) to serve downloads via apache, not PHP. I've been using the X-Sendfile (download and a simple guide here) apache module for a while in a site that servers files as big as 3GB. It works like a charm.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.