<?php
function remove_directory($directory) {
if (is_dir($directory) === true) {
$contents = scandir($directory);
unset($contents[0], $contents[1]);
foreach($contents as $object) {
$current_object = $directory.'/'.$object;
if (filetype($current_object) === 'dir') {
remove_directory($current_object);
} else {
unlink($current_object);
}
}
rmdir($directory);
}
}
?>
<h1>DELETE</h1>
<form action="" method="get">
<?php if (isset($_GET['delete']) === true) {
remove_directory('files/folder'); / when I click that submit button must delete only 'folder' not all 'folder1' and 'folder2'
}
?>
<input type="submit" name="delete"/>
</form>
<form action="" method="get">
<?php if (isset($_GET['delete']) === true) {
remove_directory('files/folder1'); // when I click that submit button must delete only 'folder1'
}
?>
<input type="submit" name="delete"/>
</form>
<form action="" method="get">
<?php if (isset($_GET['delete']) === true) {
remove_directory('files/folder2'); // when I click that submit button must delete only 'folder2'
}
?>
<input type="submit" name="delete"/>
</form>
When I click submit button I want delete only submitted file. But when I click submit button now it is delete all file in directory not onlu submitted…
When I click first button I must delete only ‘files/folder’ not all … How I can get that???? I have a function remove_directory. I know my proglem is with $_GET variable but i don’t know how I can fix it.
But now when I click just one submit button delete all folders – folder, folder1 and folder2
Where is the problem or how I can fix it?
PHP code is executed on the server, the created HTML is send to the client. Putting PHP code in your form doesn’t mean that that code will be executed on form submit. In fact it isn’t connected to the form at all.
Instead you need to send which dir should be deleted in the form using an
<input type="hidden">.Also I recommend using POST instead of GET.