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); |