Upload mp3 file and info into database how?

I create a datebase named “mp3”. Know I want to create a table called “contents” that will consist of

ID Primary Key
Song Name
Artist
Release Date
Additional Notes
File {contain mp3 file}

I will collect all this info through a PHP form. How do I load all this info into the table called “contents” and can I load mp3 files into an mysql database??

I have never dealt with MP3 file and MYSQL so I need some guidance

this is the PHP form so far

print<<<HTML
<table>
<form action=“upload.php” method=“post” enctype=“multipart/form-data”>

<tr><td><b>MP3 Info:</b></td></tr>
<tr><td>Song Name:</td><td><input type=“text” name=“name”></td></tr>
<tr><td>Artist: </td><td><input type=“text” name=“artist”></td></tr>
<tr><td>Release Date: </td><td> <input type=“text” name=“release_date”></td></tr>
<tr><td>Additional Notes</td></tr>
<tr><td></td><td><TEXTAREA NAME=“note” COLS=40 ROWS=6></TEXTAREA></td></tr>
<tr><td>Select a File to Upload:</td>
<td><input type=“file” name=“mp3_file”>
<input type=“Submit” value=“Upload File”></td></tr>

</form>
</table>

I wouldn’t recomend uploading mp3 files into a database since they are so large, but if you end up doing so, all you’d have to do is get the file contents and put it into a BLOB or LONGBLOB.

Accessing it would be just like accesing an image from a database except you’d change the filetype header settings.

I decided to create a directory on my server to store the mp3 files instead and then to store the files path in the mysql

so I would first create the table like this

$sql = CREATE TABLE mp3_files (
id int NOT NULL AUTO_INCREMENT Primary Key,
Song Name VARCHAR,
Artist Artist VARCHAR ,
Release_Date date,
Additional Notes VARCHAR,
Path ?? );

where “Path” would it me VARCHAR type?? and how would I store the absolute path of the file using PHP

Lets say I had a folder called MP3_Files on my server that would contain all my mp3 files and I uploaded an mp3 song called big poppa? How would I store this location so that when I go to retrieve this file it know the exact one??

There is an example similar to this in Kevin Yank’s book available from sitepoint. Basically the file is uploaded and stored in the mp3 directory. The url of this file created by the form processing script is then stored in the database to record where the file is.

If you need an example email me and I’ll send you a similar script where I upload images and insert the location into the database.

I made some changed to make it easier I create the directory on my server called mp3_files I am using the htm form and php script to move the mp3 files to that location. I am lost in using the move_uploaded_file function to move the file to that directory?

can someone take a look at my script and twll me what ??? should be
move_uploaded_file(???,“$folder\???” );

<?php

if ($action == “load”) {

$folder = “mp3_files”;
move_uploaded_file(???,“$folder” );

}

?>

<html>
<head>
<title></title>
</head>
<body>
<table>
<form action=“upload.php” method=“post” enctype=“multipart/form-data”>

<tr><td><b>MP3 Info:</b></td></tr>
<tr><td>Song Name:</td><td><input type=“text” name=“name”></td></tr>
<tr><td>Artist: </td><td><input type=“text” name=“artist”></td></tr>
<tr><td>Release Date: </td><td> <input type=“text” name=“release_date”></td></tr>
<tr><td>Additional Notes</td></tr>
<tr><td></td><td><TEXTAREA NAME=“note” COLS=40 ROWS=6></TEXTAREA></td></tr>
<tr><td>Select a File to Upload:</td>
<td><input type=“file” name=“mp3_file”>
<input type=“Submit” name=action value=“Load”></td></tr>
</form>
</table>
</body>
</html>

Is this what you are looking for?

move_uploaded_file($uploadedfile, $filename)

You should also confirm file type before processing the upload.

why is $uploadedfile the variable name I use??

In your form the name of the file input: <input type = “file” name = “uploadedfile”>

Change the variable $uploaded file to the input name.

Hope this helps

I am trying to use the move_uploaded_file function to copy a file from my hard drive and move it into a directory on the server. The first agrument that I used with move_uploaded_file function is $uploadedfile because in my form I have the following <input type = “file” name = “uploadedfile”> the second argument is $folder because this variable contain the location of the directory that I want to save the file to

$folder = “C:\Program Files\Apache Group\Apache2\htdocs\mp3_files”;

When I run a test of uploading a file and copying it to directory I get the following errors

Warning: move_uploaded_file(C:\Program Files\Apache Group\Apache2\htdocs\mp3_files) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\upload.php on line 6

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move ‘C:\WINDOWS\TEMP\php4.tmp’ to ‘C:\Program Files\Apache Group\Apache2\htdocs\mp3_files’ in C:\Program Files\Apache Group\Apache2\htdocs\upload.php on line 6

what am I doing wrong??? How can I correct the problem??

?php

if ($_POST[‘action’]) {

$folder = “C:\Program Files\Apache Group\Apache2\htdocs\mp3_files”;
move_uploaded_file($uploadedfile, $folder);

}

?>

<html>
<head>
<title></title>
</head>
<body>
<table>
<form action=“upload.php” method=“post” enctype=“multipart/form-data”>

<tr><td><b>MP3 Info:</b></td></tr>
<tr><td>Song Name:</td><td><input type=“text” name=“name”></td></tr>
<tr><td>Artist: </td><td><input type=“text” name=“artist”></td></tr>
<tr><td>Release Date: </td><td> <input type=“text” name=“release_date”></td></tr>
<tr><td>Additional Notes</td></tr>
<tr><td></td><td><TEXTAREA NAME=“note” COLS=40 ROWS=6></TEXTAREA></td></tr>
<tr><td>Select a File to Upload:</td>
<td><input type = “file” name = “uploadedfile”>
<input type=“Submit” name=“submit” value=“UpLoad”>
<input type=“hidden” name=“action” value=1>
</td></tr>
</form>
</table>
</body>
</html>

You need to CHMOD the directory where the files are moved to to 777 so you have “write” permission for the file.

I am doing my test on a windows 2003 sever and the folder’s permission is set to allow everyone full control/read/write . I don’t understand why I am still getting that permission error

WOW blink.gif my web host is a pain in the $%@* I decided to load the file to my web host server and I start gettin this error

Warning: SAFE MODE Restriction in effect. The script whose uid is 500 is not allowed to access /etc/passwd owned by uid 0 in /docroot/script.php on line 2

After an hour of research on google I found out this error is a result of safe mode being enabled on my hosting company web server:

I got so frustrated that I tried a new approach :

if ($_POST[‘action’]) {

$dir = “/public_html/mp3_files/”;
// create ftp connection
$ftp_conn = ftp_connect(‘mywebsite.com’);

// logon to the ftp
$ftp_log = ftp_login($ftp_conn, ‘username’, ‘password’);

// error checking
if (!ftp_conn || !ftp_log)
echo ‘There was an error connecting to the ftp’;
else
ftp_put($ftp_conn, $dir . $_FILES[‘mp3_file’][‘name’], $_FILES[‘mp3_file’][‘tmp_name’], FTP_BINARY);
echo $_FILES[‘mp3_file’][‘name’];
ftp_close($ftp_conn);

// $_FILES[‘userfile’][‘name’] would be the name you want to put in the db
}

it works but I am wondering is there anything I missed trying to use move_uploaded_file??

Anyway know to conquer the major issue storing this info in a database

How would you test to see if a certain database was present and if not creat it???

Glad to hear you found a way to get the files uploaded. What db are you using> MySQL or MSSQL?

I am using MYSQL, it’s easier to learn and it’s FREE

Speaking of database and I stuck in using if, else if statements to test the following

Check if a “mp3db” database is present
if not create the “mp3db” database
if “mp3db” is present check to see if “mp3_files” tableis present
if not create “mp3_files” table
if table is present connect to table then goto to “input.php” file

I basically need help create the layout/outline of statement for the above action.

example:

if (Check if a “mp3db” database is present){
database is present}

else
create database

etc…

First, define $database, $host , $dbuser, $dpass


[color=black]'try to connect to db[/color]

[color=black]$mysql_link = @mysql_connect($host,$dbuser,$dbpass)[/color]
 
if (! @mysql_select_db($database,$mysql_link)){
 
'db does not exist so create it here. You must have the permissions to create a new db
 
}
else
{ ' db does exist so create tables here
 
$query1 = mysql_query("CREATE TABLE table_name_here ( 'table format )")
$query2 = same_as_above_for_each_table_needed
 
	 if ($query1){
	 print "<p>xxxxx table created</p>";
	 } else {
	 $error = 1;
	 echo("<p>Can't create xxxxx table ". mysql_error() ."<p>");
	 }
 
	 if ($query2){
	 print "<p>xxxxx table created</p>";
	 } else {
	 $error = 1;
	 echo("<p>Can't create xxxxxx table ". mysql_error()."</p>");
	 }
}
 
'If table already exist you will get an error for the query so no table needs to be created there... 

thanks for the outline I create this file listed below but I am getting this error when I run it:

Can’t create mp3_files table You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name VARCHAR, Artist VARCHAR , Release_Date date, Additional

The syntax looks good to me what’s wrong with it??

<?php

$host= “localhost”;
$dbuser= “unsernamer”;
$dbpass= “password”;
$database= “mp3_db”;
$table= “mp3_files”;

$link= @mysql_connect($host, $dbuser, $dbpass)
or die ('I cannot connect to the database because: ’ . mysql_error());

if (! @mysql_select_db($database,$link)){

mysql_query(“CREATE DATABASE $database”);

}
else
{
$sql=“CREATE TABLE $table (
id int NOT NULL AUTO_INCREMENT Primary Key,
Song Name VARCHAR,
Artist VARCHAR ,
Release_Date date,
Additional_Notes VARCHAR,
Path VARCHAR )”;

$query1 = mysql_query($sql);

 if ($query1){
 print "&lt;p&gt;$table table created&lt;/p&gt;";
 } else {
 $error = 1;
 echo("&lt;p&gt;Can't create $table table ". mysql_error() ."&lt;p&gt;");
 }

}

?>

The space in ‘Song Name’, add an underscore :slight_smile:

I have two php files one check to see if a database and table is present if it’s not it create both (check.php). The other collects info from a form and then inputs that data to the database and copy the mp3 file to a location on my computer(masterupload.php).

So far the only to things I know thats working is that it will create a database or table if one is not present {check.php files does that} and that it will copy the file to a location on the server {masterupload.php does that} But the problem I am having is that it won’t copy info from the form to the table here are the two php files in question:

Why am I unable to write info from the form to the mysql table???

this is the masterupload.php whick includes an call to the check.php file via include()

<?php

if ($_POST[‘action’]) {

/$dir = “/home/nucity44/public_html/mp3_files/”;/
$dir = “C:\Program Files\Apache Group\Apache2\htdocs\mp3_files\\”;

$uploadfile = $dir . $_FILES[‘mp3_file’][‘name’];

move_uploaded_file($_FILES[‘mp3_file’][‘tmp_name’], $uploadfile);

include ‘check.php’;

$sql = “INSERT INTO $table (Song_Name, Artist, Release_Date, Additional_Notes, Path )
VALUES ($_POST[name], $_POST[artist], $_POST[date], $_POST[notes],$uploadfile)”;

mysql_query($sql);
}
?>

check.php

<?php

$host= “localhost”;
$dbuser= “usernamer”;
$dbpass= “password”;
$database= “mp3_db”;
$table= “mp3_files”;

$link= @mysql_connect($host, $dbuser, $dbpass)
or die ('I cannot connect to the database because: ’ . mysql_error());

if (! @mysql_select_db($database,$link)){

mysql_query(“CREATE DATABASE $database”);

}
else
{

if(! @mysql_query(“SELECT * FROM $table LIMIT 0,1”)){

$sql=“CREATE TABLE $table (
id INT NOT NULL auto_increment ,
Song_Name VARCHAR(255) NOT NULL ,
Artist VARCHAR(255) NOT NULL ,
Release_Date DATE NOT NULL ,
Additional_Notes VARCHAR(255) NOT NULL ,
Path VARCHAR(255) NOT NULL ,
PRIMARY KEY (id)
)” ;

mysql_query($sql);
}
}
?>

Put an error handler like I show above. That will cause the script to tell you what the problem is.