3

I am new to databases, but I want a database for my site to store mp3 files in it, so later I play them on my site by using jplayer.

They say there are two ways to store data (in this case mp3 files) in the database:

  1. By storing actual file itself in the database.
  2. By only storing particular file URL, address, location or metadata, etc.

My question is what is the right way and how can I do that in phpMyAdmin?

2

In my personal opinion (however I am new to databases as well), I would look to store the relative URL, which would be the VARCHAR from here you could store the relative path to the file (from where the script is being called from) and load the file that way.

However it may be worth writing the SQL for your tables as well. Just in case you need to alter or tweak your table structure in the beginning it makes it simpler to alter. so it would look something like this:

create database music;

use music;

create table tbl_album(
    album_id int(5) not null auto_increment,
    album_name varchar(50)  not null,
    album_artist varchar(50) not null,
    album_genre varchar(50),
    ablum_year varchar(4),
    primary key(album_id)
);

create table tbl_tracks(
    track_id int(5) not null auto_increment,
    track_number int(2),
    track_name varchar(50) not null,
    album_id int(5),
    track_url varchar(100) not null,
    primary key(track_id),
    foreign key (album_id) references tbl_album(album_id)
);

PS: If you do choose to write the SQL for your tables I would recommend MySQL workbench which is free and a nice tool to use (http://mysqlworkbench.org/)

| improve this answer | |
1

Dan Craven answered the first half of your question, and I agree that it's better to store the path to the file rather than the file itself in your database. MySQL is fully capable of storing the full MP3 file as a BLOB, but can suffer some performance problems along the way. This part is an opinion based question -- there's no universal "right way", but most people I know would store the file on disk and only the path in the database.

About the second part, phpMyAdmin is able to work with files stored in the database but that's not really what you want to do here. You'll want to develop your own custom application (or find one) to handle loading the file through jplayer. phpMyAdmin is really for database administrators to work with their database, not for end-user access to the data. You could use phpMyAdmin to upload the files in to the database if you decide to go that route, but again you'll probably want to write something customized and specific to your task at hand.

| improve this answer | |
-3
if(isset($_REQUEST['Image_Submit']))
{
        $errors= array();
        $price = $_POST['Image_Price'];
        $category = $_POST['Image_Category'];
        $cid = $_POST['cid'];
        $file_name = $_FILES["Select_Image"]["name"];
        $file_tmp = $_FILES["Select_Image"]["tmp_name"];
        $file_size = $_FILES["Select_Image"]["size"];
        $file_type = $_FILES["Select_Image"]["type"];
        $file_ext=strtolower(end(explode('.',$_FILES['Select_Image']['name'])));
        $path = "upload/".$file_name;

        $expensions= array("jpeg","jpg","png","mp3");

        if(in_array($file_ext,$expensions)=== false)
        {
            $errors[]="extension not allowed, please choose a JPEG or PNG file.";
        }

        if($file_size > 10485760)
        {
            $errors[]='File size must be excately 10 MB';
        }

        if(empty($errors)==true)
        {
            move_uploaded_file($file_tmp,"upload/".$file_name);
            echo "Success";
        }
        else
        {
            print_r($errors);
        }


        $table = $wpdb->prefix."image";
        $data = array( 
                    'image_price' => $price,
                    'cid' => $category,
                    'image_category' => $category,
                    'image_path'    => $path 
        );      

        $insert = $wpdb->insert($table, $data);
}
| improve this answer | |
  • There are easy way to store your MP3 file. and some nonsense was give the minus rating . – jaydip patel Apr 19 '16 at 6:17
  • 2
    I have no idea who downvoted you but I can guess why. Your answer is just a code, with no explanation of how or what the code does or why we need this and how it answers the question. Good answers have lots more explanation. – ypercubeᵀᴹ Apr 19 '16 at 7:25
  • 2
    A passing reader of the question will stumble upon your answer and wonder: "Is this option 1 or 2? And is this how I should do it in phpmyadmin? And what language is it? Hm, maybe Perl. Or php. Or Javascript. yeah, must be javascript." So, do you think your answer would help them? – ypercubeᵀᴹ Apr 19 '16 at 7:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.