将类别标记到我上传的多张图片中
Tag category to my multiple uploaded images
本文关键字:张图片 更新时间:2023-09-26
我正在提交一个带有多个文件上传的表单,我只想
根据上传的图像的类别对其进行区分
上传
<form action="upload.php" method="POST">
<table>
<tr>
<td>
<select name="category[]" >
<option value="13">Food</option>
<option value="15">Medicine</option>
</select>
</td>
<td>
<input type="file" multiple name="product_image[]">
</td>
</tr>
<tr>
<td>
<select name="category[]" >
<option value="13">Food</option>
<option value="15">Medicine</option>
</select>
</td>
<td>
<input type="file" multiple name="product_image[]">
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>
选择类别为"食物",并在第一行上传一张名为"food.jpg"的图片。选择类别为"medical",并在第二行上传两张名为"medicine1.jpg和medicine2.jpg"的图像提交后,我的结果将作为
Array(
[category] => Array
(
[0] => 13
[1] => 15
)
[product_image] => Array
(
[0] => food.jpg
[1] => medicine1.jpg
[2] => medicine2.jpg
)
)
但我想根据类别值标记图像。就像我想要
[product_image] => Array
(
[13] => food.jpg
[15] => medicine1.jpg
[15] => medicine2.jpg
)
一个可能的解决方案是在提交表单时创建一个隐藏的输入字段,该字段包含所选类别id和文件名的串联值。格式可以是这样的:
'id_selected_category##filename]
然后,您可以使用此分隔符(在本例中为##)从$_POST数组中检索服务器上的类别和文件名,这样您就可以知道为该图像选择了哪个类别。
本例中隐藏输入字段的名称为:
'category_file'
然后,您的$_POST
阵列将看起来像这样:
array(3) {
["category"]=>
array(2) {
[0]=>
string(2) "13"
[1]=>
string(2) "13"
}
["product_image"]=>
array(3) {
[0]=>
string(5) "1.jpg"
[1]=>
string(5) "2.jpg"
[2]=>
string(5) "3.jpg"
}
["category_file"]=>
array(3) {
[0]=>
string(9) "13##1.jpg"
[1]=>
string(9) "13##2.jpg"
[2]=>
string(9) "13##3.jpg"
}
}
例如:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['category_file'])) {
$categoryFiles = $_POST['category_file'];
foreach($categoryFiles as $categoryFile) {
$categoryAndFile = array_filter(explode("##", $categoryFile));
if (count($categoryAndFile) == 2) {
// Here you can differentiate the uploaded images according to the category which they were uploaded
$category = $categoryAndFile[0];
$file = $categoryAndFile[1];
}
}
}
}
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#theForm").submit(function() {
var categories = $('select[name=category''['']]');
$(categories).each(function() {
var category = this;
var imageNames = $(this).parent().next().children('input[type="file"]').prop("files");
$(imageNames).each(function(){
// Concatenate the selected category with the name of the image
var categoryFileName = $(category).val() + '##' + this.name;
// Add the hidden input field
$('<input>').attr({
type: 'hidden',
name: 'category_file[]',
value: categoryFileName
}).appendTo("#theForm");
});
});
});
});
</script>
</head>
<body>
<form id="theForm" action="upload.php" method="POST">
<table>
<tr>
<td>
<select name="category[]" >
<option value="13">Food</option>
<option value="15">Medicine</option>
</select>
</td>
<td>
<input type="file" multiple name="product_image[]">
</td>
</tr>
<tr>
<td>
<select name="category[]" >
<option value="13">Food</option>
<option value="15">Medicine</option>
</select>
</td>
<td>
<input type="file" multiple name="product_image[]">
</td>
</tr>
<tr>
<td>
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
if (!is_dir($dir)) {
mkdir($dir);
else
//upload image
}
创建目录
当您获得图像并准备保存到数据库中时,使用上面的代码和教程创建目录。然后将该图像上传到指定的目录路径中。
相关文章:
- 如何从三张图片中选择一张?其余的必须是看不见的
- 通过onclick事件将一张图片更改为6张图片
- 我用JavaScript FIleReader读了一张图片,但是我无法得到图片的信息
- 为多张图片添加上传前的图片预览
- HTML5画布如何在另一张图片上绘制一张图片
- 完整的背景图片幻灯片jquery,想让幻灯片的最后一张图片可以点击
- 如何在Jquery Mobile中将两张图片居中对齐
- 如何在多张图片上显示fontwo敬畏的图标
- 无法使用formData数组一次上载5张图片
- 如何使用jquery或javascript为两张图片制作动画
- 如何知道最后一张图片何时到达灯箱2
- 为什么我的 Jquery 轮播不会停留在最后一张图片上
- 下载多张图片,而无需在Chrome扩展程序中询问
- 连续设置 4 张图片而不是 2 张图片
- jQuery幻灯片,转到上一张图片
- Jquery:上传多张图片时,发布请求中断
- 使用 NodeJS 下载数百万张图片
- 检查 2 张图片是否相同
- 为什么我的js函数只找到第一张图片
- 多张图片上传,全部显示,全部拖拽