multiple Checkbox unlink

  • seularts
  • Graduate
  • Graduate
  • User avatar
  • Posts: 153
  • Loc: Romania

Post 3+ Months Ago

I have this little code right here that unlink an image when i select a checkbox and submit it to delete. is there a way to delete multiple images in the same time!?

PHP Code: [ Select ]
<?php
 
 
if(isset($_POST['rfile'])){
$img=$_POST['rfile'];
echo $img;
unlink('xxx/abc/'.$img);
}else{}
 
if (empty($file_ary))
{
    $image_path = dirname(__FILE__) . '/xxx/abc';
    $dir_handle = null;
   
    if (!($dir_handle = opendir($image_path)))
    {
 
        return;
    }
    $html = '<html><form method="post">';
    $file = readdir($dir_handle);
   
    while ($file)
    {
   if(!$file == "." || !$file == ".." || eregi("\.jpg",$file) )
        $html .= "{$file}<input type=\"checkbox\" name=\"rfile\" value=\"{$file}\" /><br />\r\n";
        $file = readdir($dir_handle);
    }
    closedir($dir_handle);
   
    $html .= '<input type="submit" name="submit" value="Delete" />';
    $html .= "</form></html>";
    echo $html;
}
 
 
if (!empty($file_ary))
{
    echo '<pre>';
    print_r($file_ary);
    echo '</pre>';
}
 
?>
  1. <?php
  2.  
  3.  
  4. if(isset($_POST['rfile'])){
  5. $img=$_POST['rfile'];
  6. echo $img;
  7. unlink('xxx/abc/'.$img);
  8. }else{}
  9.  
  10. if (empty($file_ary))
  11. {
  12.     $image_path = dirname(__FILE__) . '/xxx/abc';
  13.     $dir_handle = null;
  14.    
  15.     if (!($dir_handle = opendir($image_path)))
  16.     {
  17.  
  18.         return;
  19.     }
  20.     $html = '<html><form method="post">';
  21.     $file = readdir($dir_handle);
  22.    
  23.     while ($file)
  24.     {
  25.    if(!$file == "." || !$file == ".." || eregi("\.jpg",$file) )
  26.         $html .= "{$file}<input type=\"checkbox\" name=\"rfile\" value=\"{$file}\" /><br />\r\n";
  27.         $file = readdir($dir_handle);
  28.     }
  29.     closedir($dir_handle);
  30.    
  31.     $html .= '<input type="submit" name="submit" value="Delete" />';
  32.     $html .= "</form></html>";
  33.     echo $html;
  34. }
  35.  
  36.  
  37. if (!empty($file_ary))
  38. {
  39.     echo '<pre>';
  40.     print_r($file_ary);
  41.     echo '</pre>';
  42. }
  43.  
  44. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

I'm going to assume this is for something on a private network and ignore the gaping security hole.

First, replace this line

Code: [ Select ]
$html .= "{$file}<input type=\"checkbox\" name=\"rfile\" value=\"{$file}\" /><br />\r\n";


With this

Code: [ Select ]
$html .= "{$file}<input type=\"checkbox\" name=\"rfile[]\" value=\"{$file}\" /><br />\r\n";


That will force $_POST['rfile'] to be registered as an array instead of a single value.

Next, replace this block

Code: [ Select ]
if(isset($_POST['rfile'])){
$img=$_POST['rfile'];
echo $img;
unlink('xxx/abc/'.$img);
}else{}
  1. if(isset($_POST['rfile'])){
  2. $img=$_POST['rfile'];
  3. echo $img;
  4. unlink('xxx/abc/'.$img);
  5. }else{}


With this

Code: [ Select ]
if( ! empty($_POST['rfile']))
{
    foreach((array)@$_POST['rfile'] as $img)
    {
        echo $img;
        unlink("xxx/abc/$img");
    }
}
  1. if( ! empty($_POST['rfile']))
  2. {
  3.     foreach((array)@$_POST['rfile'] as $img)
  4.     {
  5.         echo $img;
  6.         unlink("xxx/abc/$img");
  7.     }
  8. }
  • seularts
  • Graduate
  • Graduate
  • User avatar
  • Posts: 153
  • Loc: Romania

Post 3+ Months Ago

Thanks, it worked like a charm :)

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 79 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.