Mike Huebner
Mike Huebner

Reputation: 669

Delete Specific Image According to Id created dynamically

I got a quick question. I am displaying images in a specific session directory through PHP and also deleting them, problem is, the script deletes all of the images not just one of them. Here is what I'm doing:

PHP/HTML:

<ul class="image-list">
        <?php
        if ($_SESSION['curHotelId']) {
            $count = 1;
            foreach(glob('assets/php/upload/'.$_SESSION['curHotelId'].'/*') as $filename){
                echo '<li id="del'.$count.'" class="image-list"><img src="'.$filename.'" width="50px" height="50px"/><p class="filename">' . basename($filename) . '</p> <a class="btn btn-mini btn-primary image-list" style="width: 50px; margin-top:-20px;" id="del'.$count.'" value="Delete">remove</a></li>' . "\n" . "<br>";
                $count++;
            }
        }else {}
        ?>
</ul>

jQuery:

$('ul li a.image-list').live('click', function() { 
    var answer =  confirm('Are you sure you wish to delete this image?');
    if (answer) {
    $.post('assets/php/deletefile.php');
    $(this).closest('.li').remove();
    }
});

Delete Script:

<?php 
session_start();
    foreach(glob("upload/" . $_SESSION['curHotelId'] . "/" . '*.*') as $file)
        if(is_file($file))
            @unlink($file);
?>

Anything helps!

Upvotes: 1

Views: 294

Answers (1)

ozahorulia
ozahorulia

Reputation: 10094

Remove only one (first) file in current session folder:

<?php 
session_start();
$files = glob("upload/" . $_SESSION['curHotelId'] . "/" . '*.*');

if(is_file($files[0]))
    @unlink($files[0]);

?>

Upvotes: 1

Related Questions