unlink() multiple 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
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

unlink() multiple files

Post by dhrosti »

im having trouble finding anything about this on google, so i hope you guys can help.

I need to find a way to use unlink() or something similar to delete all files that start with a certain string.

Not sure how to go about this.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Something like this:

Code: Select all

<?php
$dir = $_SERVER["DOCUMENT_ROOT"]."/folder/";
if ($dh = opendir($dir)) 
{
    while (($file = readdir($dh)) !== false) 
    {
        if (substr($file, 0, 3) == "xxx")
        {
	    unlink($file);
        }
    }
    closedir($dh);
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

glob() and unlink() combo can easily accomplish this
Post Reply