我正在制作一个表单来上传照片,而且我有多个no。输入类型的“文件”字段在单个表单下具有不同的字段名称。表单提交后多输入类型的'文件'字段变空
此外,我使用ajax发送这些文件。我是PHP新手,我在搜索解决方案时花了大约6个小时的时间,并且处理了很多变体,但无法解决。
我面临的问题是只有一个字段的文件传递给PHP,而其他字段为空。文件移动成功后只获取一个字段的输出。在检查'print_r'后,我发现这些数组是空的。
我的图片文件不超过250 KB和JPG。那么没有任何文件大小和类型问题。
HTML代码(从PHP呼应AJAX请求后):
<form action=\"upload.php\" method=\"POST\" enctype=\"multipart/form-data\" class=\"upload-inputs\">
<div class=\"upload-wrap\">
<input placeholder=\"Choose a file..\" class=\"file-name\" type=\"text\">
<input type=\"file\" name=\"file1\" id=\"file\" class=\"inputfile\">
<label for=\"file\">Browse</label>
</div>
<div class=\"upload-wrap\">
<input placeholder=\"Choose a file..\" class=\"file-name\" type=\"text\">
<input type=\"file\" name=\"file2\" id=\"file\" class=\"inputfile\">
<label for=\"file\">Browse</label>
</div>
<div class=\"upload-wrap\">
<input placeholder=\"Choose a file..\" class=\"file-name\" type=\"text\">
<input type=\"file\" name=\"file3\" id=\"file\" class=\"inputfile\">
<label for=\"file\">Browse</label>
</div>
<button class=\"manage-btn upload-btn\" type=\"submit\">Upload</button>
</form>
<div class=\"upload-alert\"></div>
JQuery的& AJAX:
$(document).on('submit','.upload-inputs', function(e) {
e.preventDefault();
$.ajax({
url : "upload.php",
method: "POST",
data : new FormData(this),
processData: false,
contentType: false,
success:function(mesg)
{
$(".upload-alert").html(mesg);
}
});
});
不使用多个文件选择,因为在移动浏览器(Chrome)检查后,我发现,它不允许在手机中选择多个文件。所以使用不同的单个输入字段。
'upload.php的' 代码:
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// Having this same code for 'file2' & file3 below (Just changing the variable names to file2 & file3)
if(!empty($_FILES['file1']))
{
$file1 = $_FILES['file1'];
$file1_name = $file1['name'];
$file1_tmp = $file1['tmp_name'];
$file1_error = $file1['error'];
$file1_size = $file1['size'];
$file1_size = $file1_size/1024;
$file1_size = number_format($file1_size, 0);
$file1_ext = explode('.', $file1_name);
$file1_ext = strtolower(end($file1_ext));
$allowed = array('jpg');
if (in_array($file1_ext, $allowed))
{
if($file1_error === 0)
{
if($file1_size <= 2048)
{
$file1_name_new = uniqid(rand()) . ".$file1_ext";
$file1_destination = 'profile_image/'.$file1_name_new;
// Output if successfully moved.
if (move_uploaded_file($file1_tmp, $file1_destination))
{
echo " <div class=\"files-bar1\">
<button class=\"manage-btn delete-image\">Delete</button>
<img class=\"image-thumb\" src=\"profile_image/$file1_name_new\">
<label class=\"image-name\">$file1_name_new</label><br>
<p class=\"image-size\">$file1_size KB</p><br>
<p class=\"percentage\">_% Completed</p><br/>
<progress class=\"upload-progress\" min=\"0\" max=\"100\"></progress>
<label class=\"stop-uploading\">✖</label>
</div>";
}
else
{
echo "Sorry, something went wrong. Please try again later.";
}
}
else
{
echo "Image Size Must Be Under 2 MB.";
}
}
else
{
echo "Caught Error.";
}
}
else
{
echo "Only JPG/JPEG format is allowed.";
}
}
else
{
echo "file1 Undefined.";
}
}
好了的话,你是提供没有工作对我来说是Ajax代码..我不知道你是怎么设法拿到了产量,但对我来说,当我上传的图片我正在收到当我的if条件返回false时设置为接收的消息“Undefined file1”。 但有一点拍了拍我,你告诉我,我不能用同一个ID的所有字段。所以我用我的工作AJAX代码,更改ID和它的工作。 另外,我使用的斜线在HTML,因为我从一个php文件输出HTML形式。”我想坚持多单输入字段的原因。 不管怎样,现在它的作品。谢谢! –