I need to put together a quick script that will clean up a directory full of pictures for me. I want it to just delete photos that are not actively being used by members. I'm familiar with the different aspects involved in doing this but a little unsure of how to put it all together.

A simple query to select pics currently in use:

$sql=mysql_query("SELECT pic FROM user_profile_pics WHERE  pic !=''");
while($r=mysql_fetch_array($sql)){
	$pic=$r['pic']; }

Listing the contents of the dir.

$dirname = "some_dir";
$dir = opendir($dirname);

while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") ) // Edit
{
echo("$file <br />");
}
}

And of course I've used unlink() to give files the axe.
The problem is that I tested a few things out with unexpected results and I really need to get it right before I set it loose on an active members db.

If I had to put it into a query (would be nice if this really worked) it would look something like this:

$sql=mysql_query(" unlink * FROM some_directory WHERE $file NOT IN (some_array) ");

Seems simple enough. Why the heck can't I come up with something solid that actually works?

Still haven't quite figured this one out. Any ideas?

Member Avatar for GreenDay2001

I think unlink() is the only function to delete a file. What problems are you facing with that. Usually people face problem when there is no suffcient permissions.

unlink works just fine. I'm just stuck with a coding issue. There are a few hundred photos in the directory at the moment and I'm trying to put together a script that reads what photos are actively being used and deletes the photos from the directory that are not actively being used by a member. I can write the script that reads what photos are being used, I can write the script to read all files in the directory, and I can unlink a file, but what I'm having problems with is putting it all together. My example above pretty much sums up what I'm looking to do.

Member Avatar for diafol

IF I get you correctly, you've got a members table with a current avatar field. You need to check that any files not in the table are removed from the directory.

Pseudocode:

for each file in directory start loop
query db - if not listed in table - delete
end loop

If that's what you need - all well and good, but you're querying the db every time you come across a file - could be heavy. So, you could put all the db filenames into an array and just check the physical filenames against the array:

Pseudocode:

1) get records into array
2) for each file in directory start loop
if not in array - delete (unlink)
end loop

Is that right? If you need proper code instead of pseudocode let me know.

I got sidetracked from that code for a little but I'll get back to it tonight. I did try a foreach but i wasn't doing something correctly because it was going after files I did not want it to. It was just a test, I didn't actually unlink anything at that point ... that would have made a fine mess.

Pseudo code is fine (and yes, what you have there is exactly what I'm trying to do) ... I'm heading in the right direction, just hit a few speed bumps along the way. I'll take another crack at it tonight and let you know how I make out this time around.

Ardav, I'm running into problems with the arrays.

Scandir loads the contents into an array so this is not a prob.

$directory = "some_dir";
  $contents = scandir($directory);
    if ($contents) {
       foreach($contents as $key => $file) {
		   
		   echo "$file <br />";
       }
    }

The query is what is giving me problems.

$sql=mysql_query("SELECT pic FROM user_profile_pics WHERE  pic !='' AND pic !=0");
while($r=mysql_fetch_array($sql)){
$pic[]=$r['pic'];}

Also, in_array() is pretty straightforward but how do I go about NOT in_array(). I can make NOT IN work within a query but not really sure how to pull it off outside of the query.

Member Avatar for diafol
$directory = "some_dir";
$contents = scandir($directory);
//all files in $contents array

$sql=mysql_query("SELECT pic FROM user_profile_pics WHERE  pic <>'' AND pic <> 0");
while($r=mysql_fetch_array($sql)){
   $pic[]=$r['pic'];
}
//this should give all the used files

for($i=0;$i<count($contents);$i++){
  if(!in_array($contents[$i],$pic)){
     unlink($directory . "/" . $contents[$i]);
  }
}
//I think this'll work

Although there may be a more efficient way of doing things. Also check the unlink($directory . "/" . $contents[$i]) line - use echo instead of unlink to check: echo $directory . "/" . $contents[$i].

I'll give that a shot and see what happens. I'm assuming <> is the same as != ? Also, this is what I was looking for if(!in_array ... don't know why I didn't think to try that.

Thanks Ardav, It worked beautifully! Had to make a few minor adjustments so it wasn't trying to delete subdirectories. I definitely learned something on how to load a result set into an array, I was having some real problems there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.