Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting file that corresponds to the ID in the database 1

Status
Not open for further replies.

sharapov

MIS
May 28, 2002
106
US
Let's say I have the following records in mySQL database. ID, name, adress, phone_number. I also have images that correspond to to the record IDs. (For example: if records ID 12345 corresponding image is 12345.jpg)

From time to time I delete records from my database, but image file stays on the server. So what I want to do is search through all the records in the database and compare it to the existent files. If ID in the database matches the file on the server I don't want to do anything, if there is a file on the server, but there is no corresponding record (ID), I want to delete that file and finally if there is record in the database but there is no corresponding file, I want to list those records (IDs) on the page.

I would really appreciate if anybody would help me with this.
 
I had to do something similar once. Here's my code:

Code:
<?php

$mysql_server = &quot;localhost&quot;;
$mysql_user = &quot;test&quot;;
$mysql_password = &quot;test&quot;;
$mysql_db = &quot;test&quot;;

$directory_name = &quot;.&quot;;

$basic_query = &quot;SELECT count(filename) FROM filelist where filename = '&quot;;

print &quot;<html><body><pre>&quot;;

$dbh = @mysql_connect ($mysql_server, $mysql_user, $mysql_password);

if ($dbh !== FALSE)
{
	if (@mysql_select_db($mysql_db, $dbh))
	{
		$dirh = @opendir ($directory_name);
		
		if ($dir !== FALSE)
		{
			while ($filename = readdir($dirh))
			{
				if (preg_match (&quot;/\.gif$|\.jpg/i&quot;, $filename))
				{
					$query = $basic_query . $filename . &quot;'&quot;;
					
					$rsh = mysql_query ($query, $dbh);
					
					if ($rsh !== FALSE)
					{
						list ($match_count) = mysql_fetch_array ($rsh);
						
						if ($match_count == 0)
						{
							print &quot;deleting $directory_name/$filename\n&quot;;
							unlink ($directory_name . &quot;/&quot; . $filename);
						}
					}
					else
					{
						print mysql_error();
					}
				}
			}
		}
		else
		{
			print &quot;Could not open directory&quot;;
		}
		
		closedir ($dirh);
		
		$query = &quot;SELECT id, filename FROM filelist&quot;;
		
		$rsh = mysql_query ($query, $dbh);
		
		if ($rsh !== FALSE)
		{
			while ($record_data = mysql_fetch_array ($rsh))
			{
				if (! file_exists ($directory_name.&quot;/&quot;.$record_data[1]))
				{
					print &quot;Record &quot;.$record_data[0].&quot; has missing image\n&quot;;
				}
			}
		}
		else
		{
			print mysql_error();
		}
	}
	else
	{
		print mysql_error();
	}
}
else
{
	print mysql_error();
}

print &quot;</pre></body></html>&quot;;

?>
______________________________________________________________________
TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top