0

I'm trying to allow user to delete images from a folder on server through html form and PHP.

Here's my html form markup along with PHP script generating list images from the folder mentioned before. I've added checkboxes with path+filename as value.

<form action="delete.php" method="POST">
    <div id="formlist">

            <?php
            $path = ".";
            $dh = opendir($path);
            $i=1;
            $images = glob($path."*.png");
            while (($file = readdir($dh)) !== false) {
                if($file != "." && $file != ".." && $file != "index.php" && $file != "form.css" && $file != ".htaccess" && $file != "error_log" && $file != "cgi-bin") {
                    echo "<div class='formshow'><a href='$path/$file' data-lightbox='Formularze' data-title='$file'><img class='formimg' src='$path/$file' width='500px'/></a><input type='checkbox' name='deleteform' value='$path/$file'></div>";
                    $i++;
                }
            }
            closedir($dh);
            ?> 

    </div>
    <input type="submit" value="Usun zaznaczone formularze">
</form>

Now, here's my delete.php file:

<?php
            $path = ".";
            $dh = opendir($path);
            $i=1;
            $deletepath = glob('deleteform');
            while (($file = readdir($dh)) !== false) {
                    unlink($deletepath);
                    $i++;
            }
?>

I keep this error:

Warning: unlink() expects parameter 1 to be a valid path, array given

I'm quite green with PHP, so I decided to ask you guys - how may I make this work? Should i unserialize() it and add [0], [1] counters?

Damian Doman
  • 522
  • 8
  • 19
  • `vardump` your `$deletepath` first and make sure its not an array but a string. – Bagus Tesa Apr 13 '17 at 02:35
  • @BagusTesa no matter how many checkboxes I check, I always get ' string(20) "./1492033022form.png" ' – Damian Doman Apr 13 '17 at 02:37
  • 1
    *no matter how many checkboxes I check,* -- well, read the [legendary question on multiple checkbox input](http://stackoverflow.com/questions/4997252/get-post-from-multiple-checkboxes) yet? basically, change `type='checkbox' name='deleteform' value='$path/$file'>` to `type='checkbox' name='deleteform[]' value='$path/$file'>` - your parameter will be an array then and you can loop it out. – Bagus Tesa Apr 13 '17 at 03:04
  • @BagusTesa worked! ^ :) – Damian Doman Apr 13 '17 at 12:17
  • well, glad it worked! – Bagus Tesa Apr 15 '17 at 12:15

1 Answers1

0

To delete all itens in a folder user this:

 $directory = "folder/";
     if ($cat_handle = opendir($directory)) {
       while (false !== ($entry = readdir($cat_handle))) {
           @unlink($directory.$entry);
       }
      closedir($cat_handle);
     }

You need delete itens or folder? if you need delete specific item:

$file_name = "fulano.jpg";
 if ($handle = opendir('folder/')) {
    while (false !== ($entry = readdir($handle))) {
     if ($entry != "." && $entry != "..") {
        if($entry == $file_name){
          @unlink('folder/'.$name);
         }
     }
 }
  closedir($handle);
}

I believe it works no seu caso, change to accept array now.