SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP File Function

Deleting all files of a directory

We have seen how a file can be deleted by using unlink function in PHP. The same function can be used along with directory handler to list and delete all the files present inside. We have discussed how to display all the files present inside a directory. Now let us try to develop 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.
Related Tutorial
Delete file


Here is the code to this.
function EmptyDir($dir) {
$handle=opendir($dir);

while (($file = readdir($handle))!==false) {
echo "$file <br>";
@unlink($dir.'/'.$file);
}
closedir($handle);
}
EmptyDir('images');
Here images is the directory name we want to empty

Deleting Selected files through checkboxes

As we know how to display all files of a directory , we will use same concept to fist display the list of files. While displaying the file names we will add one checkbox before it for selection of file by user for deletion. For this we have used one array of checkbox.

echo "<form method=post name='f1' action=''>";
while (list ($key, $val) = each ($ar)) {
if(strlen($val)>3){
echo "<input type=checkbox name=box[] value='$val'>$val<br>";
}
}
echo "<input type=submit value='Delete'></form>";

On submit of the form we can loop through the array of checkbox to identify files which are selected by user.

while (list ($key,$val) = @each ($box)) {
$path=$dir ."/".$val;
if(unlink($path)) echo "Deleted file ";
echo "$val,";
}


Then we will select the path and delete the selected files by using unlink command.

Here is the complete code.

<?Php
$dir='test_dir'; // directory name
$ar=scandir($dir);
$box=$_POST['box']; // Receive the file list from form

// Looping through the list of selected files ///
while (list ($key,$val) = @each ($box)) {
$path=$dir ."/".$val;
if(unlink($path)) echo "Deleted file ";
echo "$val,";
}
echo "<hr>";

/// displaying the file names with checkbox and form ////
echo "<form method=post name='f1' action=''>";
while (list ($key, $val) = each ($ar)) {
if(strlen($val)>3){
echo "<input type=checkbox name=box[] value='$val'>$val<br>";
}
}
echo "<input type=submit value='Delete'></form>";
?>

Marking for deletion

We can modify the above script and instead of deleting the file we can display them with strikeout text showing marked for deletion. On further confirmation from the user same can be deleted.

Number of User Comments : 1


Google+
dhiraj chavan10-10-2014
nice...thanx to plus2net
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer