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

ファイルのアップロードに使用jsonしているため、ファイルの削除にも使用できます。削除する画像 URL の配列を送信して、サーバーに別のajax呼び出しを行います。次に、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 に答える
0

うーん、私のホスティング プロバイダの plesk パネルは 5.3.3 しかサポートしていないので、fileinput スクリプトには 5.3.3 以上の php バージョンが必要です。すべてがうまくいきます!

于 2015-09-04T04:30:05.547 に答える