943,987 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1134
  • PHP RSS
Feb 5th, 2007
0

Files linked to MySQL records

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Feb 7th, 2007
0

Re: Files linked to MySQL records

[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.
Reputation Points: 13
Solved Threads: 2
Junior Poster
php_daemon is offline Offline
138 posts
since Aug 2006
Feb 7th, 2007
0

Re: Files linked to MySQL records

Click to Expand / Collapse  Quote originally posted by php_daemon ...
[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.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005
Feb 8th, 2007
0

Re: Files linked to MySQL records

Worked great. Thanks for the help.
Reputation Points: 10
Solved Threads: 0
Posting Whiz in Training
nathanpacker is offline Offline
234 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Choosing File Upload Destination from dropdown
Next Thread in PHP Forum Timeline: web scraping





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC