How can PHP be used to create a form with checkboxes or select fields for users to select files for deletion?
To create a form with checkboxes or select fields for users to select files for deletion using PHP, you can first list the files in a directory and display them as checkboxes or options in a select field. When the form is submitted, you can use PHP to process the selected files for deletion based on the user's input.
<?php
// Directory where files are located
$directory = "path/to/files/";
// Get list of files in the directory
$files = scandir($directory);
// Display form with checkboxes for each file
echo '<form method="post">';
foreach($files as $file){
if($file != '.' && $file != '..'){
echo '<input type="checkbox" name="filesToDelete[]" value="'.$file.'">'.$file.'<br>';
}
}
echo '<input type="submit" name="deleteFiles" value="Delete Selected Files">';
echo '</form>';
// Process form submission
if(isset($_POST['deleteFiles'])){
if(isset($_POST['filesToDelete'])){
foreach($_POST['filesToDelete'] as $fileToDelete){
$filePath = $directory.$fileToDelete;
if(file_exists($filePath)){
unlink($filePath);
echo $fileToDelete.' has been deleted.<br>';
} else {
echo $fileToDelete.' does not exist.<br>';
}
}
} else {
echo 'No files selected for deletion.';
}
}
?>
Related questions
-
PHPMailer Swift Mailer mail() function
-
PHP5 MySQL database connection
-
PHP email compatibility