【发布时间】:2013-07-16 20:45:54
【问题描述】:
我有一个简单的问题。我正在通过 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);
?>
任何帮助!
【问题讨论】:
-
这是因为您在
glob()中使用了*.*。你预计会发生什么?您根本没有尝试将要删除的特定文件名作为 POST 数据发送。 -
你的
glob命令搜索这个:'*.*'。这是文件夹中的每个文件。然后它会删除它找到的所有内容。 -
我知道这就是它的作用。我把它作为占位符放在那里,因为我不知道如何访问特定文件。对此我深表歉意,我认为这就是我正在做的事情。
标签: php javascript jquery file session