Jump to content

[SOLVED] File delete from check-boxes with dynamic not working


anolan13

Recommended Posts

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!

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>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.