Bulk deletion should be limited to a known directory and a controlled filename pattern. Never accept an arbitrary directory path from a request and delete everything inside it.
$dir = __DIR__ . '/temp';
foreach (glob($dir . '/*.tmp') ?: [] as $file) {
if (is_file($file)) {
unlink($file);
}
}This example shows the recommended starting pattern. Continue below for variations, explanation and the original practical examples.
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
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,";
}
<?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>";
?>
$ar=scandir($dir);We are only once using this and after deleting we can again check the directory by using scandir function. So you can add this line after deleting the files.
<?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>";
$ar=scandir($dir);// Once again directory content is scanned to exclude deleted files.
/// 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>";
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| dhiraj chavan | 10-10-2014 |
| nice...thanx to plus2net | |
| amir | 01-03-2017 |
| thanks. the files are deleted, but it has some warning. it can't read 'box'. why ? | |
| smo1234 | 03-03-2017 |
| Not clear what is the box here, do you have any file name or any thing else. Tested the script it is working fine. After deleting the same old files are shown again. So the script is modified to take a fresh scan of directory after deleting the files. | |