Files linked to MySQL records

Reply

Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Files linked to MySQL records

 
0
  #1
Feb 5th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 138
Reputation: php_daemon is an unknown quantity at this point 
Solved Threads: 2
php_daemon php_daemon is offline Offline
Junior Poster

Re: Files linked to MySQL records

 
0
  #2
Feb 7th, 2007
[php]
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";
}
}
[/php]
Last edited by php_daemon; Feb 7th, 2007 at 2:54 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Files linked to MySQL records

 
0
  #3
Feb 7th, 2007
Originally Posted by php_daemon View Post
[php]
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";
}
}
[/php]
Thanks! I will give that a try.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Files linked to MySQL records

 
0
  #4
Feb 8th, 2007
Worked great. Thanks for the help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC