This is code asked for a bandsname and is suppose to check the bands folder and compare it to the bands table in the database. If it finds a file in the folder and not in the database it should update the database with that file. If it finds entries in the database without a matching file in the folder it should delete the row in the database. I thought it was working fine, but I was wrong. I am in the process of trying to figure it out, but so far no luck.
This is what I get when I put two new files in the folder and hit summit.
Processing /bandname/Test
'test1.mp3' added to file
'test2.mp3' added to file
These were deleted from the file
test1.mp3
test2.mp3
It should just update the database. It seems to be updating and the deleting it? Any help would be appreciated.
<?php
mysql_connect("localhost","name","pass");
mysql_select_db("mp3s");
if (isset($_POST['submit'])) {
mysql_connect("localhost","name","pass");
mysql_select_db("mp3s");
$bandname = $_POST['bandname'];
// set dir name
$dir = "full/path/to/$bandname/songs"; # specify full path here
echo "Processing $dir<br>";
// open directory
$dir = opendir($dir);
// loop through the directory while reading it's contents
while ($file = readdir($dir)) {
// if the current file in the loop is a mp3, then inset into database
if (ereg("mp3$", $file)) {
$title = $file;
$fileurl = "http://www.mysite.com/$bandname/songs/$file ";
$result = mysql_query("SELECT COUNT(*) FROM songs WHERE title LIKE '$title'");
if (mysql_result($result,0) == 0) {
// not found, so add to table
mysql_query("INSERT INTO songs (artist,filename,title)
VALUES ('$bandname', '$fileurl', '$title')");
echo "'$title' added to file<br>";
}
else {
// store title of found file
$mp3_array[] = $title;
}
}
}
// now delete the ones that weren't found
$mp3list = "'" . join("','", $mp3_array) . "'";
echo 'These were deleted from the file<br>';
$res = mysql_query ("SELECT title FROM songs WHERE title NOT IN ($mp3list)");
while ($r = mysql_fetch_row($res)) {
echo $r[0] . '<br>';
}
mysql_query ("DELETE FROM songs WHERE title NOT IN ($mp3list)");
}
?>
<form method='post'>
Enter name of band <input type='text' name='bandname'>
<input type='submit' name='submit' value='Submit'>
</form> :quiet: