Deleting all files of a directory using php

by Nithya 2011-06-08 16:07:23



The unlink function can be used along with directory handler to list and delete all the files present inside. Here is a function and to this function we will post directory name as parameter and the function will use unlink command to remove files by looping through all the files of the directory.

Here is the code to this.

function deleteAllFiles($dir) {
$handle=opendir($dir);

while (($file = readdir($handle))!==false) {
echo "$file
";
@unlink($dir.'/'.$file);
}

closedir($handle);
}

deleteAllFiles('images');


Here images is the directory name we want to empty

Tagged in:

736
like
0
dislike
0
mail
flag

You must LOGIN to add comments