anolan13 Posted August 2, 2007 Share Posted August 2, 2007 Hi all, I need to display all the files in a directory, number them, provide them each with check boxes, after the user clicks the check boxes and clicks the submit button (delete) the corresponding files with those checked boxes are deleted... This is the code I've tried using - echo "<ol>\n"; echo "<form action='process.php' method='POST'>\n"; $handle=opendir("files/"); while (($file = readdir($handle))!==false) { echo "<li><a href='http://www.mywebsite.com/$name/files/$file'>$file</a></li> <br>"; $file++ == $file1; echo "<input type='checkbox' name='$file1' value='$file'>"; } closedir($handle); echo "</ol>\n"; echo "<input type='submit' value='Delete'>\n"; echo "</form\n"; Well that makes it look right but it's just not working... here's process.php <?php $_POST['$file1']['$file'] unlink($file) header("Location: http://www.google.com"); ?> Is there a problem somewhere? Or am I just doing it completely wrong? Is there a better way of doing this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/63109-solved-file-delete-from-check-boxes-with-dynamic-not-working/ Share on other sites More sharing options...
btherl Posted August 3, 2007 Share Posted August 3, 2007 Try adding var_dump($_POST) at the top of your script, to see what is in the $_POST array. Quote Link to comment https://forums.phpfreaks.com/topic/63109-solved-file-delete-from-check-boxes-with-dynamic-not-working/#findComment-314598 Share on other sites More sharing options...
bibby Posted August 3, 2007 Share Posted August 3, 2007 Give your checkboxes the same name and use brackets to make them into an array You want in your form, echo "<input type='checkbox' name='delete[]' value='$file'>"; ..and for your handler if(is_array($_POST['delete'])) foreach($_POST['delete'] as $filepath) if(file_exists($filepath)) unlink($filepath); else echo "can't find $filepath <br />\n"; Here's a whole test page for that. <? var_dump($_POST); ?> <form method="post"> <input type="checkbox" name="delete[]" value="1"> <input type="checkbox" name="delete[]" value="2"> <input type="checkbox" name="delete[]" value="3"> <input type="submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/63109-solved-file-delete-from-check-boxes-with-dynamic-not-working/#findComment-314678 Share on other sites More sharing options...
shen Posted August 3, 2007 Share Posted August 3, 2007 rewrite your code as follows echo "<ol>\n"; echo "<form action='process.php' method='POST'>\n"; $handle=opendir("files/"); while (($file = readdir($handle))!==false) { echo "<li><a href='http://www.mywebsite.com/$name/files/$file'>$file</a></li> <br>"; // $file++ == $file1; echo "<input type='checkbox' name='$file1[]' value='$file'>"; } closedir($handle); echo "</ol>\n"; echo "<input type='submit' value='Delete'>\n"; echo "</form\n"; and the process scripts is if($_POST[file1]) { $filearr = $_POST[file1]; foreach ($filearr as $value) { unlink($value); } } header("Location: http://www.google.com"); Quote Link to comment https://forums.phpfreaks.com/topic/63109-solved-file-delete-from-check-boxes-with-dynamic-not-working/#findComment-314679 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.