DizzyThermal Posted September 19, 2010 Share Posted September 19, 2010 Hey guys, I've written a script that is suppose to Display the files within a specified directory /documents/ with check boxes to the right of them.. This worked out great. Then I wanted to devise a method to remove or unlink the selected files (via check box) when the user pressed a Submit button.. File Permissions on the server are 777 and I have hardcoded an unlink to test to see if it works and it does.. So the bug lies within my code.. Before I post the code let me explain my method in doing this. I created a for loop to name each check box after the corresponding file to the left and created a variable with the file name (to use later) Then on button press I made a for loop (it goes up to 50 because I wasn't sure how to make it the exact number of files and I didn't want to overload the server with say 1000000 :?) that goes through and checks the check box's values and if they equal 1, unlink the file in /documents/ Here's the code: <html> <body> <form action="" method="post" class="delete"> <?php $dir = "documents/"; if ($handle = opendir($dir)) { echo '<h2>List of Files in '.$dir.'</h2>'; echo '<table>'; for ($i = -2; false !== ($file = readdir($handle)); $i++) { if ($file != "." && $file != "..") { echo '<tr><td><a href="'.$dir.''.$file.'">'.$file.'</a></td><td><input type="checkbox" name="Del['.$i.']" /></td</tr>'; $DelFile[$i] = $file; } } echo '</table>'; closedir($handle); } ?> <br /> <input type="submit" name="submit" value="Delete Checked Files" /> </form> <?php for ($i = 0; $i <= 50; $i++) if ('Del['.$i.']' == 1) unlink("'.$dir.''.$DelFile[$i].'"); ?> </body> </html> Help is extremely appreciated, and Thanks in advance guys! Quote Link to comment https://forums.phpfreaks.com/topic/213775-unlink-files-in-a-directory/ Share on other sites More sharing options...
Mod-Jay Posted September 19, 2010 Share Posted September 19, 2010 You dont need the '.$DelFile[$i].' Quote Link to comment https://forums.phpfreaks.com/topic/213775-unlink-files-in-a-directory/#findComment-1112660 Share on other sites More sharing options...
DizzyThermal Posted September 19, 2010 Author Share Posted September 19, 2010 At what point in the code is this unneeded? Quote Link to comment https://forums.phpfreaks.com/topic/213775-unlink-files-in-a-directory/#findComment-1112666 Share on other sites More sharing options...
PFMaBiSmAd Posted September 19, 2010 Share Posted September 19, 2010 It would be much simpler if you use the filename as the name="Del['. ... .']" array index name. That way you won't need to pass the $DelFile[$i] from the code that makes the form to the form processing code. You also need to condition the form processing code so that it is ONLY executed when the form is submitted. You are kind of lucky that the code you posted did not delete all your files the first time you requested the page (to produce the form.) Edit: Also, if this is a real application (not just a learning experience), you will need to add some security to insure that the current visitor has the necessary permissions to both list the files in the form and delete the files in the form processing code. Quote Link to comment https://forums.phpfreaks.com/topic/213775-unlink-files-in-a-directory/#findComment-1112669 Share on other sites More sharing options...
Pikachu2000 Posted September 19, 2010 Share Posted September 19, 2010 I'd set up the checkboxes as an array with the filename as the value= attribute, then loop through the array for the unlink. As PFMaBiSmAd mentioned above, there are security concerns whenever you allow filesystem manipulation through a web based form, and you should obviously do all testing on a directory that's set up for that purpose. <html> <body> <form action="" method="post" class="delete"> <?php $dir = "documents/"; if( isset($_POST['submit']) ) { // echo '<pre>'; // print_r($_POST); // echo '</pre>'; foreach( $_POST['file'] as $v ) { if ($v != '.' && $v != '..' ) { unlink( "$dir/$v" ); } } } if ($handle = opendir($dir)) { echo '<h2>List of Files in '.$dir.'</h2>'; echo '<table>'; for ($i = -2; false !== ($file = readdir($handle)); $i++) { if ($file != "." && $file != "..") { echo "<tr><td><a href=\"$dir$file\">$file</a></td><td><input type=\"checkbox\" name=\"file[]\" value=\"$file\" /></td</tr>"; } } echo '</table>'; closedir($handle); } ?> <br /> <input type="submit" name="submit" value="Delete Checked Files" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/213775-unlink-files-in-a-directory/#findComment-1112674 Share on other sites More sharing options...
PFMaBiSmAd Posted September 19, 2010 Share Posted September 19, 2010 Slightly different method, same results - <?php $dir = "documents/"; if(isset($_POST['submit']) && isset($_POST['Del'])){ foreach($_POST['Del'] as $key => $value){ unlink($dir.$key); } } ?> <html> <body> <form action="" method="post" class="delete"> <?php if ($handle = opendir($dir)) { echo '<h2>List of Files in '.$dir.'</h2>'; echo '<table>'; while($file = readdir($handle)) { if ($file != "." && $file != "..") { echo '<tr><td><a href="'.$dir.''.$file.'">'.$file.'</a></td><td><input type="checkbox" name="Del['.$file.']" /></td</tr>'; } } echo '</table>'; closedir($handle); } ?> <br /> <input type="submit" name="submit" value="Delete Checked Files" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/213775-unlink-files-in-a-directory/#findComment-1112675 Share on other sites More sharing options...
DizzyThermal Posted September 19, 2010 Author Share Posted September 19, 2010 Awesome, thanks guys, is there an advantage to using one script over the other? Like speed wise? EDIT: Also, it's showing directories in the list.. This is kinda cool, but I can't distinguish between files and documents.. I believe unlink doesn't do documents right? Quote Link to comment https://forums.phpfreaks.com/topic/213775-unlink-files-in-a-directory/#findComment-1112678 Share on other sites More sharing options...
PFMaBiSmAd Posted September 19, 2010 Share Posted September 19, 2010 You would need to add an is_file test in your if ($file != "." && $file != "..") conditional test if you only want to list files. If you want your code to actually transverse folders within your documents/ folder and list the files in each folder, you would need to use a recursive function. Quote Link to comment https://forums.phpfreaks.com/topic/213775-unlink-files-in-a-directory/#findComment-1112830 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.