0

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 ?

4

2 回答 2

0

uuuhh,fileinput 脚本需要 php 版本高于 5.3.3,因为我的托管服务提供商的 plesk 面板只支持 5.3.3 我有问题,因为 11.5 plesk 支持服务器上每个域的多个 php 版本,现在使用 php 5.6一切都很好!

于 2015-09-04T04:30:05.547 回答
0

由于您json用于上传文件,因此您也可以使用它来删除它们。ajax通过发送要删除的图像 URL 数组再次调用服务器。然后使用 PHP,您可以简单地取消链接它们。

例如: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
});
于 2015-08-30T08:51:25.170 回答