0

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?

2
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by nathanpacker
0
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";
  }
}
0
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.