gpt4 book ai didi

javascript - 多图上传不起作用

转载 作者:行者123 更新时间:2023-12-03 05:07:38 24 4
gpt4 key购买 nike

我想一次上传多张图片,但不起作用。它仅上传所选的第一张图像。我不知道下面的代码有什么问题。我添加了 javascript 标签

上传.php

if (isset($_POST['upload'])) {
$j = 0; // Variable for indexing uploaded image.
$target_path = "uploads/"; // Declaring Path for uploaded images.
for ($i = 0; $i < count($_FILES['file']['name']); $i++) {

// Loop to get individual element from the array

$validextensions = array(
"jpeg",
"jpg",
"png"
); // Extensions which are allowed.
$ext = explode('.', basename($_FILES['file']['name'][$i])); // Explode file name from dot(.)
$file_extension = end($ext); // Store extensions in the variable.
$target_path = $target_path.md5(uniqid()).
".".$ext[count($ext) - 1]; // Set the target path with a new name of image.
$j = $j + 1; // Increment the number of uploaded images according to the files in array.
if (($_FILES["file"]["size"][$i] < 10000000) // Approx. 10MB files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {

// If file moved to uploads folder.

echo $j.
').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
} else { // If File Was Not Moved.
echo $j.
').<span id="error">please try again!.</span><br/><br/>';
}
} else { // If File Size And File Type Was Incorrect.
echo $j.
').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
}

index.php

<html>
<head>
<script type="text/JavaScript" src="js/script.js"></script>
</head>
<body>
<form method="post" action="upload.php">
<div id="filediv">
<input type="file" id="file" name="file[]">
</div>
<br>
<input type="button" id="add_more" class="btn btn-primary" value="Add More Images" />
<input name="upload" id="upload" type="submit" value="Upload">
</form>
</body>
</html>

脚本.js

var abc = 0; // Declaring and defining global increment variable.
$(document).ready(function() {
// To add new input file field dynamically, on click of "Add More Files" button below function will be executed.
$('#add_more').click(function() {
$(this).before($("<div/>", {
id: 'filediv'
}).fadeIn('slow').append($("<input/>", {
name: 'file[]',
type: 'file',
id: 'file'
}), $("<br/>")));
});
// Following function will executes on change event of file input to select different file.
$('body').on('change', '#file', function() {
if (this.files && this.files[0]) {
abc += 1; // Incrementing global variable by 1.
var z = abc - 1;
var x = $(this).parent().find('#previewimg' + z).remove();
$(this).before("<div id='abcd" + abc + "' class='abcd'><img id='previewimg" + abc + "' src=''/></div>");
var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
$(this).hide();
$("#abcd" + abc).append($("<img/>", {
id: 'img',
src: 'images/x.png',
alt: 'delete'
}).click(function() {
$(this).parent().parent().remove();
}));
}
});
// To Preview Image
function imageIsLoaded(e) {
$('#previewimg' + abc).attr('src', e.target.result);
};
$('#upload').click(function(e) {
var name = $(":file").val();
if (!name) {
alert("First Image Must Be Selected");
e.preventDefault();
}
});
});

最佳答案

您应该在循环内声明 count($_FILES['file']['name']) 。您已对外声明

$count = count($_FILES['file']['name']);
for ($i = 0; $i < $count; $i++) {
// your code come here
}

关于javascript - 多图上传不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41959652/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com