Page 1 of 1

Deleting images

Posted: Mon Dec 29, 2008 11:04 am
by Schneider707
I wrote this function about a day ago (I just started to learn php only 4 days ago...so go easy on me =P ). Basicall, right now this script simply scans a folder, then posts all the images it finds between li tags. What I would like to do is write another function (to be used on a seperate page) that would add checkboxes with a delete button or something to that affect that would allow deletion of a single image or of multiple images (hence why I thought a check mark system would be best).

I understand using unlink to delete the images, but I'm still lost at the point of printing a checkbox for each image still.

Also, on a side note. This script is setup because I have another script that when you upload an image, it creates a thumbnail of it as well and stores it in a seperate folder. This script actually finds all of the thumbnails, then populates the page with those, and links each image to their respective full sized image. Hope that makes sense =)

Code: Select all

<?php
 
function pagepop(){
            
            $imglist = scandir("images/thumb_nails");
            $arr = array($imglist);
                    $length = count($imglist);
                        
                    for($x=2; $x<$length; $x++){
                    foreach($arr as $image){
                        
                        $path_to_thumb = 'images/thumb_nails/' . $image[$x];
                        $path_to_full = 'images/full_sized/' . $image[$x];
                        
                            // list of approved file extensions
                            $approved = array (
                                'gif' => "image/gif",
                                'jpg' => "image/jpeg",
                                'jpeg' => "image/jpeg",
                                'png' => "image/png"
                            );
                            
                            $ext = pathinfo($image[$x]);
                                                
                            if(array_key_exists($ext['extension'], $approved)){
                                echo "<li class =\"thumb_li\"><a href =\"$path_to_full\" id=\"remove\"><img src=\"$path_to_thumb\" class=\"thumbnail\" /></a></li>";
                                }
                        
                    }
                } 
            }
 
?>
 

Re: Deleting images

Posted: Mon Dec 29, 2008 11:24 am
by Schneider707
Ok so I actually got the checkboxes to print with the delete button...but how do I actually get the delete button to actually delete something??

Code: Select all

$ext = pathinfo($image[$x]);
                                                
                            if(array_key_exists($ext['extension'], $approved)){
                                echo "<li class =\"thumb_li\"><a href =\"#\" id=\"remove\"><img src=\"$path_to_thumb\" class=\"thumbnail\" /></a><input type=\"checkbox\" name=\"delete[$x]\" value=\"delete\"></li>";
                                }
                    
                    }
                } echo "<input type=\"submit\" value=\"Delete\" name=\"Delete\">";
            }
 

It may seem really obvious to you guys but I think I've just been starring at the screen to come up with a logical process to figure this out.

Re: Deleting images

Posted: Mon Dec 29, 2008 11:41 am
by mmj
You'd have to make a form then loop thought the checked check boxes and call unlink.

Re: Deleting images

Posted: Mon Dec 29, 2008 8:05 pm
by Schneider707
Ok deleted all of this post.

So here is what I've got right now. Let me know if I need to post the entire function.

Code: Select all

    if(array_key_exists($ext['extension'], $approved)){
                                echo "<div class=\"img_holder\"><a href =\"#\" id=\"remove\"><img src=\"$path_to_thumb\" class=\"thumbnail\" /></a><input type=\"checkbox\" name=\"checkbox[]\" value=\"'.$delete.'\"></div>";
                                }
                           } 
            }echo "<p><input type=\"submit\" value=\"Delete\" name=\"Delete\" ></p>";   
                            
                            if(isset($_POST[checkbox])){
                                        $valuearray = $_POST[checkbox];
                                            foreach ($valuearray as $key=>$value) {
                                                if($value = 1){
                                                unlink($image[$x]);                         
                                                    }
                                                      }
                                                        }
So basically I want the last bit to check for all of the checked boxes and then delete the ones that are. However, when I check a box and hit the delete button, it says unlink() no file or directory found. So I'm assuming its just the $image[$x] part that doesn't work. Normally I would put $path_to_thumb (this equals images/thumb_nails . $x) in there which works just fine to populate the page with all the images...but for some reason that doesn't want to work.

So anyone have an idea on how to link to the images? Or is that gonna end up being another 20 lines of code? Btw, $path_to_thumb is within the same function and when I replace $image[$x] with something like images/thumb_nails/crow142667.jpg and I select that image as well as others, it will delete only that image. <---I understand thats obvious, but I wanted to show that I'm sure all of the code is correct minus the unlink argument.
:banghead:

Re: Deleting images

Posted: Tue Dec 30, 2008 1:12 am
by Schneider707
anyone? I tried to follow mmj's suggestion, only got this far with a seperate function

Code: Select all

<?php function checkbox(){
    if($_POST[submit]){
        $checkbox = $_POST[checkbox];
        $arr = array($checkbox);
            foreach($arr as $check){
                if($check = 1){
                unlink($check);                                                                                                 }   
}                                                                                                                   }           
}                                                                                                                                               ?>
But that function seems absolutely useless as I can't see how to get it to delete the respective image of the checkbox.

I've looked around quite a bit through different forums and all I can find is people trying to delete images from their databases (why even put images in a database?)