PHP Script – Checking for New Files in a Directory

This script checks for new files in a directory by comparing the directory to a database of file names.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//open the directory
$handle = opendir("/home/brentstrysko/..your folder");
 
//connect to the project's database
mysql_connect("my database url", "username", "password") or die(mysql_error());
mysql_select_db("the database you want to use") or die(mysql_error());
 
//go through all the files in the directory
while(false !== ($fileName = readdir($handle)))
{
	//if the file is not junk
	if(($fileName != '.') && ($fileName != '..'))
	{
		//so the search starts from the beginning
		$result = mysql_query("SELECT * FROM list");
 
		$alreadySet = false; 				
 
		//go through all the database filenames and if the current file is in the database dont add it again 
		while($row = mysql_fetch_array($result))
		{
			if($row["songName"] == $fileName)
			{
				$alreadySet = true;
				break;
			}
		}
 
		//if the file is not in the database add it
		if(!$alreadySet)
		{
			mysql_query("INSERT INTO list (fileName) VALUES(" . $fileName .") or die(mysql_error());
		}
	}
}
 
echo "Directory update completed<br />";
closedir($handle);

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

*
What is 6 + 15 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human)

WordPress Themes