php - 使用动态输入字段将文件和文件类型插入mysql数据库
问题描述
我正在尝试使用创建的动态输入字段将多个文件插入数据库。有什么方法可以将文件类型插入数据库?我尝试过使用下面的代码,但只能插入第一个文件。
PHP
if($q >= 0){
for($i=0; $i<=$q; $i++){
$certcheck = true;
$upload = end(explode(".", $_FILES['cert']['name'][$i]));
$imageType = $_FILES['cert']['type'][$i];
$fileType = array("png", "jpg", "jpeg", "pdf");
if (in_array(strtolower($upload), $fileType)) {
$cert1 = file_get_contents($_FILES['cert']['tmp_name'][$i]);
}
else {
$error = " The file is not an image or pdf. Please upload again";
$certcheck = false;
$_SESSION['error'] = $error;
}
$cert = $cert1;
$dtype = $imageType;
$sql3="INSERT INTO shorttest (cert,dtype) VALUES (:cert,:dtype)";
$query = $dbh->prepare($sql3);
$query->bindParam(':cert', $cert, PDO::PARAM_STR);
$query->bindParam(':dtype', $dtype, PDO::PARAM_STR);
$query->execute();
$msg = "Your Application Has Been Sent !";
}
}
}
风格
var q = 0;
function qualification() {
q++;
var copyContent= "<div class='row' name='rows' style='margin-top:-20px;'>";
copyContent= "<tr><td><input class='tableBody' type='file' id='cert"+q+"' name='cert[]'></td>";
copyContent += "<td><a href='javascript:void(0);' class='remove' style='cursor:pointer'><i class='material-icons' title='Delete item'>remove_circle_outline</i></a></td></tr></div>";
$('#tbl_qualification').append(copyContent);
document.getElementById("q").value = q;
}
HTML
<table id="tbl_qualification"style="margin-top:25px;margin-bottom:25px;">
<tr class="tableTitle">
<th style="width:200px"><center>Document</th>
<th style="width:20px;"></th>
</tr>
<tr>
<td><input class="tableBody" type="file" id="cert0" name="cert[]"></td>
</tr>
</table>
<div style="margin-right:-1200px">
<a onclick="qualification()" style="cursor:pointer"><i class="material-icons" title="Add item">add_circle_outline</i></a>
</div>
<input name="q" type="text" id="q" readonly hidden>
解决方案
推荐阅读
- android-layout - 'coordinatorLayoutStyle' 出现新的 android studio 渲染问题
- c++ - Linux 上的 curl_easy_perform 分段错误
- spring - 限制用户每篇文章发表一定数量的评论
- google-apps-script - 将条件格式传输到文档
- server - 让 PHP 和普通用户编辑相同文件的三种方法
- python - Keras:输入层并正确传递输入数据
- git - 将 Git 分支推送到远程而不签出分支
- laravel - 如何处理来自 Laravel 支付网关的数组
- lambda - PySpark UDF 中是否存在 lambda
- python - 如何使用 opencv 和 python 翻转图像(没有 cv2.flip)