Jump to content

[SOLVED] Need to lst user select file(s) in a directory and delete them.


jej1216

Recommended Posts

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

 

 

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

  • 2 weeks later...

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.