1

我有一个快速的问题。我通过 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);
?>

什么都有帮助!

4

1 回答 1

1

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

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

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

?>
于 2013-07-16T20:50:31.450 回答