首页 > 解决方案 > 如何创建从目录中删除多个文件/图像的功能?

问题描述

这就是我目前尝试设置该功能的方式:

/** Deletes any images and associated thumbnails */
function deleteImages() {
$_POST['deleteImage'] = array();
$selectedImages = $_POST['deleteImage'];
foreach($selectedImages as $selectedImage) {
unlink(GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/".$selectedImage);
unlink(GALLERY_IMG_PATH."/".GALLERY_IMG_THUMB."/".$selectedImage);
}
}

我正在使用以下内容在图库中显示表单和图像:

<form action="Admin?admin_action=<?php echo $results['formAction']?>" 
method="post" enctype="multipart/form-data" name="formUploadFile">
<?php
    $directory = "GALLERY_IMG_PATH."/".GALLERY_IMG_FULLSIZE."/";
    $images = glob("$directory*.{jpg,png,webp}", GLOB_BRACE);
    foreach($images as $image) {
        $filepath = $image;
        $filename = pathinfo($filepath, PATHINFO_FILENAME);
        $basename = pathinfo($filepath, PATHINFO_BASENAME);
?>
<label class="centered_label" for="deleteImage" title="Click here to delete 
current image.">Delete Image:<input type="checkbox" name="deleteImage" 
id="deleteImage" value="<?php echo $basename?>"></label>
<img id="postImage" src="<?php echo $image;?>" title="<?php echo 
preg_replace('/\_/', ' ',$filename)?>." alt="<?php echo 
preg_replace('/\_/', ' ',$filename)?>.">
<?php } ?>
<label class="centered_label" for="images[]" title="This field is to upload 
an image/images for the Image Gallery.">Select image/images to 
upload</label>
<input type="file" name="images[]" id="images" accept="image/*" 
multiple="multiple" placeholder="Upload an image/images.">
<input type="submit" name="uploadImages" value="UploadImages/SaveChanges" 
title="Click/tap here to save/upload image(s)/save changes. You will be 
returned to Admin Homepage.">
<input class="cancel" type="button" name="cancel" value="Cancel" 
title="Click/tap here to cancel and exit Image Gallery Uploader. You will 
be returned to Admin Homepage." formnovalidate onclick="history.go(-1)">
</form>

我在这里编辑了我的原始帖子,以反映我如何尝试删除图库编辑表单的图像。我似乎无法让它工作。但是,当我单独选择一个时,我可以让它工作,在这种情况下,全尺寸和缩略图都会被删除。这就是我想要的每次删除。我遇到的问题是当我尝试选择多个图像时。以上是尝试的deleteImages function()我无法开始工作。

标签: phpfunction

解决方案


推荐阅读