how can i delete all files in a folder

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
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

how can i delete all files in a folder

Post by itsmani1 »

Hi
I want to delete all .html files in current directory using php code?
any solution?

thanks a lot.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

glob() and unlink() may be of interest.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I would look at opendir() and readdir()

http://us2.php.net/manual/en/function.readdir.php
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

I would do something like this:

Code: Select all

$dir_handle = @opendir('path') or die('Cannot read the path.');
while ($file = readdir($dir_handle)) 
{
	if(is_file($file) and strpos($file,'.html'))
	{
		unlink($file);
	}
}
closedir($dir_handle);
Post Reply