jej1216 Posted November 28, 2007 Share Posted November 28, 2007 I know how to list files in a directory on the server: <?php $dir = "./FORMS/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "<a href=\"".$dir.$file."\">".$file."</a><br/>"; } closedir($dh); } } ?> and I know how to use unlink() to delete a specified file in a directory on the server: <?php $dir = "./FORMS/"; $file = "myform.pdf"; $myFile = $dir . $file; $fh = fopen($myFile, 'w') or die("can't open file"); fclose($fh); $myFile = $dir . $file; unlink($myFile); echo "Deleted: ".$myFile; ?> Now I need to give the user the ability to select multiple files in the directory and then delete them. What's the best approach to doing this? I'm hoping to be able to put a checkbox next to each file and then have a delete function, but I'm too much of a novice to see how to do this. TIA, jej1216 Quote Link to comment Share on other sites More sharing options...
MadTechie Posted November 28, 2007 Share Posted November 28, 2007 create a form.. now list all files and add a check box as an array with the values equal to the filename ie echo "<input type=\"checkbox\" name=\"delfile[]\" value=\"$file\" /> $file<br />"; now add a submit button ie echo "<input name=\"delfiles\" type=\"submit\" value=\"Delete\" />"; now when you click submit the checkbox array is passed, this contains the filenames ie <?php $BasePath = "Folder/of/files"; if(isset($_POST['delfiles'])) { foreach($_POST['delfile'] as $df) { echo "$BasePath/$df"; //debug #unlink("$BasePath/$df"); } ?> this should help.. if you are still stuck just post what you have so far and we're try to help.. i could post a compleated script but learning is better Quote Link to comment Share on other sites More sharing options...
jej1216 Posted November 29, 2007 Author Share Posted November 29, 2007 Thanks! I agree, I want to build the code and then better understand what it does. I'll try this and follow-up with either a success or other issues. jej1216 Quote Link to comment Share on other sites More sharing options...
jej1216 Posted December 11, 2007 Author Share Posted December 11, 2007 Just wanted to close this thread - your direction was great - I got the code working. Here is my finished code: <form name="delchecked" enctype="multipart/form-data" action="ehi_delete_these.php" method="POST"> <?php $dir = '/usr/local/FORMS/' . $_POST['folder'] . '/'; echo "Folder is: ".$_POST['folder']; echo "<br/>"; function shortimg($str){ list($short)=explode(" ",$str,2); return $short; } if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if($file !== "." && $file !== ".." ) $short=shortimg($file); $aryRef[]=$short; $files[$short]=$file; } sort($aryRef,SORT_NUMERIC); foreach ($aryRef as $key) { $file=$files[$key]; if($file !== "." && $file !== ".." ) { echo "<input type='checkbox' name='checkthis' value='delete'/>"; echo "<input type='text' size=100 name='filename' value='".$file."'/><br>"; } } closedir($dh); } } ?> <p><input type="submit" value="Delete Checked Files" /></p> </form> I ended up using 2 arrays as directed by another PHP guru, one to get the short name, the other to hold the full name. Thanks for the help! Quote Link to comment 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.