Delete certain picture files
Moderator: General Moderators
Delete certain picture files
I have a mysql database that has picture url's in it
IE: http://www.domain/images/pic1.jpg
these url's are for pictures people have uploaded. along the way certain mysql entries are deleted. Problem is now i have a folder full of pictures that are not related to any user posts.
Can Anyone think of a way to delete the actual image files that do not correspond to any url's in the database?
IE: http://www.domain/images/pic1.jpg
these url's are for pictures people have uploaded. along the way certain mysql entries are deleted. Problem is now i have a folder full of pictures that are not related to any user posts.
Can Anyone think of a way to delete the actual image files that do not correspond to any url's in the database?
somthing like:
Code: Select all
<?php
$folder = "my_images/";
$handle = opendir($folder);
while($files = readdir($handle))
{
if($files != "." && $files != "..")
{
$check = mysql_num_rows(mysql_query("Select image from table where name = '$files' limit 1"));
if($check == 0)
{
//might need to chmod the files to 0777
unlink($folder.$files);
}
}
}
?>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Ok i must admit im fairly new to php (programing in general really), the only problem i see is that the images are stored with full path in the database ie: http://www.domain/images/pic1.jpg and the names i get from looping them out are not :ie pic1.jpg.
how do i go about either adding the full path in the loop or doing some way i have not thought about LOL
how do i go about either adding the full path in the loop or doing some way i have not thought about LOL
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: