Page 1 of 1
Delete certain picture files
Posted: Sat Oct 09, 2004 9:14 am
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?
Posted: Sat Oct 09, 2004 9:20 am
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);
}
}
}
?>
Posted: Sat Oct 09, 2004 11:22 am
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
Posted: Sat Oct 09, 2004 11:42 am
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
Posted: Sat Oct 09, 2004 11:44 am
by twigletmac
You can use the [php_man]basename[/php_man]() function to get the filename out of the URL.
Mac
Posted: Sat Oct 09, 2004 12:15 pm
by Cammet
Ok Thanx For Everything

, If i can ask? How did you guys learn php. (I need some direction lol)
Posted: Sat Oct 09, 2004 12:19 pm
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
Posted: Sat Oct 09, 2004 12:27 pm
by feyd
I learned everything from the manual.. and previously knowing C/C++ helped heaps, as it were.
Posted: Sat Oct 09, 2004 5:02 pm
by qads
hamsters..they are very knowledgeable

.
I am a book person, reading before going to sleep helps me

...and i dont need a pillow

.
Posted: Sat Oct 09, 2004 5:25 pm
by John Cartwright
qads wrote:...and i dont need a pillow

.
how sick and twisted must some one be to not need a pillow!!!
