根据动态创建的Id删除特定图像

时间:2013-07-16 20:45:55

标签: php javascript jquery file session

我有一个简单的问题。我通过PHP在特定会话目录中显示图像并删除它们,问题是,脚本删除所有图像而不仅仅是其中一个图像。这就是我正在做的事情:

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();
    }
});

删除脚本:

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

一切都有帮助!

1 个答案:

答案 0 :(得分:1)

仅删除当前会话文件夹中的一个(第一个)文件:

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

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

?>