0

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.

  • 3
    Store the file under a unique name in a folder outside the web root and store the unique name and actual name in your database. Use a script to retrieve and serve the file with the correct name when it is requested. –  May 02 '14 at 08:24
  • 1
    the easiest solution would be if you rename ALL the files - thats what all big upload services do - and give each of them the owner in the database. Another way would be to add the username in front of it, for example `"User1Frontliner-Symbols"` and then remove the Username in front of it when he accesses it. Username is usually unique, so there cant be doubled names. – Realitätsverlust May 02 '14 at 08:24
  • I did the similar thing in my project, when a new file with same comes i just simply rename it. For eg. ring.mp3 already exist the new file will be ring_1.mp3 and again ring.mp3 came it will be ring_2.mp3 – Vijay Singh Rana May 02 '14 at 08:26
  • possible duplicate of [allow UTF-8 encoded filenames on (file-)webserver?](http://stackoverflow.com/questions/23083685/allow-utf-8-encoded-filenames-on-file-webserver) (your question comes from a different angle, but the solution is the same) – deceze May 02 '14 at 08:30

1 Answers1

0
You can try by appending the Unix time stamp at the end of the file name.

like this you can use:

<?php
$date = new DateTime();
$val= $date->getTimestamp();// it will give the unix time stamp
?>

append $val to the end of your file.

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44