Jump to content

Unlink files in a directory


DizzyThermal

Recommended Posts

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!

 

:)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

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.