Delete an array of files from a form

Submitted by:David Villa

Date added:15 September, 2012

Category:PHP

This will perform the action of deleting many files.

Tags: delete an array

Code Snippet:

  # form.html

<!-- These files must exist to work (This is only an example) -->
<form action="process.php" method="post">
<input type="checkbox" value="file1.html" name="delete[]"> file1.html<br>
<input type="checkbox" value="file2.html" name="delete[]"> file2.html<br>
<input type="checkbox" value="file3.html" name="delete[]"> file3.html<br>
<input type="submit" value="Delete Selected" name="submit">
</form>



# process.php

<?php
# Take each checked value, and give it a name of $delete
foreach($_POST['delete'] as $delete){
# Make sure the file actually exists
if(file_exists("/location/of/directory/".$delete)){
# Delete each file
unlink("/location/of/directory/",$delete);
}else{
continue;
}
}
# Return to form.html
header("Location: form.html");
?>
 
 

Comments