I am building a music uploading system in PHP. I want the user to be able to upload a song to the server and of course be able to access it later. I have been informed that storing the mp3 files in a server directory is more efficient than storing it in a database. I have set up tables for the user account, the mp3 files and for albums like so
create table users (
user_id int(11) NOT NULL AUTO_INCREMENT,
user_name varchar(64) NOT NULL,
user_firstname varchar(64) NOT NULL,
user_lastname varchar(64) NOT NULL,
user_email varchar(64) NOT NULL,
user_registration_date datetime NOT NULL,
PRIMARY KEY (user_id),
UNIQUE KEY (user_name),
UNIQUE KEY (user_email)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE tracks (
user_id INT(11) NOT NULL,
track_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
album_id INT UNSIGNED NOT NULL,
track_name varchar(64) NOT NULL,
track_file_name varchar(64) NOT NULL,
track_artist varchar(20) NOT NULL,
tag1 varchar(20) DEFAULT NULL,
tag2 varchar(20) DEFAULT NULL,
tag3 varchar(20) DEFAULT NULL,
genre varchar(20) DEFAULT NULL,
sub_genre varchar(20) DEFAULT NULL,
full_length ENUM('Yes', 'No') NOT NULL,
description TEXT,
PRIMARY KEY (track_id),
INDEX (track_id),
INDEX (track_name)
) ENGINE = INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE albums (
user_id INT(11) NOT NULL,
album_name varchar(64) NOT NULL,
album_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
album_artist varchar(20) NOT NULL,
album_art_file_name varchar(64),
INDEX (album_id),
INDEX (album_name)
) ENGINE = INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
This is where I'm stuck, I can store the files in a directory and have a file path stored in the database (note: I only store the name of the file in my table for versatility in the future, like if I need to change the path where the files are stored) but what if someone uploads a file with the same name? Should I rename the file to include its ID? Is that even safe to do since it'll be show in the URL on request? Is there a better way, am I going about this horribly wrong? Thank you for your help.