Delete certain picture files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Cammet
Forum Newbie
Posts: 22
Joined: Thu Sep 23, 2004 8:00 am

Delete certain picture files

Post by Cammet »

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?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

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);
}
}
}
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You could also do it slightly differently - by getting all the image names from the database first and putting them into an array then checking each filename in the images folder against this array.

Mac
Cammet
Forum Newbie
Posts: 22
Joined: Thu Sep 23, 2004 8:00 am

Post by Cammet »

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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can use the [php_man]basename[/php_man]() function to get the filename out of the URL.

Mac
Cammet
Forum Newbie
Posts: 22
Joined: Thu Sep 23, 2004 8:00 am

Post by Cammet »

Ok Thanx For Everything :), If i can ask? How did you guys learn php. (I need some direction lol)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I kinda taught myself with help from Google and the manual but that's because I learn better if I have an actual project to work on so tutorials aren't so good for me :)

Mac
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I learned everything from the manual.. and previously knowing C/C++ helped heaps, as it were.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

hamsters..they are very knowledgeable :P.

I am a book person, reading before going to sleep helps me :roll:...and i dont need a pillow :P.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

qads wrote:...and i dont need a pillow :P.
how sick and twisted must some one be to not need a pillow!!! :twisted:
Post Reply