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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

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.