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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器