dpb35161 2015-08-30 07:00
浏览 572

bootstrap fileinput,显示上传的文件并删除它们

how can i show and delete previously uploaded files with the great bootstrap-fileinput plugin from krajee, my code is:

html:

<script>
$("#images").fileinput({
    uploadAsync: true,
    uploadUrl: "upload.php"
}).on("filebatchselected", function(event, files) {
$("#images").fileinput("upload");
});
</script>

upload.php:

<?php
if (empty($_FILES['images'])) {
echo json_encode(['error'=>'No files found for upload.']);
return;
}
$images = $_FILES['images'];
$success = null;
$paths= [];
$filenames = $images['name'];
for($i=0; $i < count($filenames); $i++){
$ext = explode('.', basename($filenames[$i]));
$target = "uploads" . DIRECTORY_SEPARATOR . basename($filenames[$i]);
if(move_uploaded_file($images['tmp_name'][$i], $target)) {
    $success = true;
    $paths[] = $target;
} else {
    $success = false;
    break;
}
}
if ($success === true) {
$output = ['uploaded' => $paths];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];
foreach ($paths as $file) {
    unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}
echo json_encode($output);
?>

Has anyone an idea ? i think i have to scan the uploads dir and send it back with json or use $output, but i dont know how to this ?

  • 写回答

2条回答 默认 最新

  • douyan1944 2015-08-30 08:51
    关注

    Since you are using json to upload files, you can use it to delete them too. Make another ajax call to the server by sending an array of the image URLs that you want to remove. Then with PHP you can simply unlink them.

    So for example: http://jsfiddle.net/fdzsLa0k/1/

    var paths = []; // A place to store all the URLs
    
    // Loop through all images
    // You can do it for a single image by using an id selector and skipping the looping part
    $('.uploaded-img').each(function(i, v) {
        paths.push(this.src); // Save found image paths
    })
    console.log(paths); // Preview the selection in console
    
    // Send the URLs to the server for deletion
    $.ajax({
        method: 'post',
        data: { images: paths },
        url: 'ajax.php' // Replace with your ajax-processing file
    }).success(function(response) {
        console.log(response); // Do fun stuff: notify user, remove images from the loaded HTML
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘