Hey, just need a little help on this one. I have a script allows a user to choose from a list of files in a directory for a form. When they submit the form, it adds the files name and url to a couple fields in the database for that entry.

What I need to do is every now and then, I need to see if any of the files in that directory are currently in use in any records in the database. In other words, I need to compare each of the file names with the column "file_name" in my database, and test to see if there are any matches. This way I will be able to clean out any old, unused files.

If the query returns no results for any one of the files, I'll know I can delete that file.

Any ideas?

chdir("path/to/directory");
$files=glob("*");
foreach ($files as $file){
  $result=mysql_query("select * from files where file_name='".mysql_real_escape_string($file)."'");
  if(mysql_num_rows($result)==0){
     echo "$file is not in use";
  }
}
chdir("path/to/directory");
$files=glob("*");
foreach ($files as $file){
  $result=mysql_query("select * from files where file_name='".mysql_real_escape_string($file)."'");
  if(mysql_num_rows($result)==0){
     echo "$file is not in use";
  }
}

Thanks! I will give that a try.

Worked great. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.