phpphreak Posted May 31, 2011 Share Posted May 31, 2011 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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/237949-php-download-script-for-downloading-mp3s-from-mysql-database-not-working/ Share on other sites More sharing options...
seanlim Posted May 31, 2011 Share Posted May 31, 2011 Try opening up the mp3 file in a text editor, and you will probably see some error message inside? Quote Link to comment https://forums.phpfreaks.com/topic/237949-php-download-script-for-downloading-mp3s-from-mysql-database-not-working/#findComment-1222770 Share on other sites More sharing options...
Fadion Posted May 31, 2011 Share Posted May 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/237949-php-download-script-for-downloading-mp3s-from-mysql-database-not-working/#findComment-1222872 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.