我试图让这个样本为我工作。
http://webtips.krajee.com/ajax-based-file-uploads-using-fileinput-plugin/
将显示图片,但我只得到每张图片的响应“内部错误”(没有进一步的信息),也没有上传文件。
的index.html
<input id="images" name="images[]" type="file" multiple=true class="file-loading">
<script>
var $input = $("#images");
$input.fileinput({
uploadUrl: "upload.php",
uploadAsync: false,
showUpload: false,
showRemove: false,
minFileCount: 1,
maxFileCount: 5,
overwriteInitial: false,
uploadExtraData: function() {
return {
userid: 100,
username: 'thomas'
};
}
}).on("filebatchselected", function(event, files) {
$input.fileinput("upload");
});
upload.php的
<?php
if (empty($_FILES['images'])) {
echo json_encode(['error'=>'No files found for upload.']);
return;
}
$images = $_FILES['images'];
$userid = empty($_POST['userid']) ? '' : $_POST['userid'];
$username = empty($_POST['username']) ? '' : $_POST['username'];
$success = null;
$paths= [];
$filenames = $images['name'];
for($i=0; $i < count($filenames); $i++){
$ext = explode('.', basename($filenames[$i]));
$target = "uploads" . DIRECTORY_SEPARATOR . md5(uniqid()) . "." . array_pop($ext);
if(move_uploaded_file($images['tmp_name'][$i], $target)) {
$success = true;
$paths[] = $target;
} else {
$success = false;
break;
}
}
if ($success === true) {
save_data($userid, $username, $paths);
$output = [];
} 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);
?>
有没有人知道什么是错的?