I need to compare two arrays and delete files associated with them. Here's the specs:

Array $files is made up of a file list from a directory on my server (~140,000 of them). Array $listingIDs is made up of one row of a database table.

What I need to do is see what filenames from $files appear in $listingIDs. Any that appear in $listingIDs get left alone. Any that don't appear in $listingIDs need to have the files associated with them deleted.

I have figured out everything except this. For some reason I just can't come up with a good way to do it.

Any help would be greatly appreciated.

Thanks in advance.

Matt

    $diff = array_diff($listingIDs, $files);
    foreach ($diff as $value)
    {
    	$unlink = unlink('/path/to/' . $value);
    	if ($unlink) {echo $value . ' delete OK<br>';}
    	else {echo $value . ' delete FAIL<br>';}
    }
    

      Thanks for the nudge. I read up on array_diff and I think the arrays being compared may be backwards. Can you tell me if I'm right?

      I need to take all the elements of $files and delete them if they don't show up in $listingIDs.

      Thanks again.

        You're right. Reverse the array order in devinemke's "array_diff()" statement. Just a typo on his part, no doubt.

          Write a Reply...