上传多个文件pdo [英] upload multiple files pdo
本文介绍了上传多个文件pdo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何也使用此脚本上载数据库中的多个文件 我想使输入文件和数据库不是必需的
how to upload multiple files in database with this script also i want to make the input file and database not required
您怎么看?脚本受保护了吗?
What do you think? Is the script protected?
这是我的php pdo脚本:
Here's my php pdo script:
$file = $_FILES['file'];
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg', 'jpeg', 'png', 'pdf');
$formErrors = array();
if (in_array($fileActualExt, $allowed)) {
if ($fileError === 0) {
if ($fileSize < 1000000) {
$fileNameNew = uniqid('', true).".".$fileActualExt;
$fileDestination = 'images/' .$fileNameNew;
move_uploaded_file($fileTmpName, $fileDestination);
} else {
$formErrors[] = "file size must be under 2mb";
}
} else {
$formErrors[] = 'error';
}
} else {
$formErrors[] 'file not uploaded..!';
}
这是html:
<tr>
<td><label class="control-label">Profile Img.</label></td>
<td><input class="input-group" type="file" name="file" multiple accept="image/*" /></td>
</tr>
插入数据库
if (empty($formErrors)) {
$stmz = $con->prepare("INSERT INTO reports(title, username, email, mobile, repdate,photo, descr)
VALUE(:ztitle, :zusername, :zemail, :zmobile, NOW(), :uphoto, :zdescr)");
$stmz->execute(array(
'ztitle' => $title,
'zusername' => $username,
'zemail' => $email,
'zmobile' => $mobile,
'uphoto' => $fileNameNew,
'zdescr' => $descr
));
/* echo "<meta http-equiv='refresh' content='0'>"; */
if ($stmz) {
echo '<script language="javascript">';
echo 'alert("your report Added")';
echo '</script>';
echo '<div class="alert alert-danger">your report Added</div>';
}
} else {
echo '<script language="javascript">';
echo 'alert("your report not Added")';
echo '</script>';
echo '<div class="alert alert-danger">your report not Added</div>';
}
推荐答案
上载多个文件时,应为多个文件创建一个数组. name ="file []"
When you are uploading multiple files then you should create an array for multiple files. name="file[]"
我建议您使用该基本类来上传多个文件.
I suggest you to use this basic class for uploading multiple files.
https://github.com/AzCreativeWorld/Dropzone-with-Image-Resizer
像这样的Tyr可以进行多次上传.
Tyr like this for multi uploading.
foreach($_FILES['files']['name'] as $val)
{
$s++;
$filesName = str_replace(" ","",trim($_FILES['files']['name'][$n]));
$files = explode(".",$filesName);
$File_Ext = substr($_FILES['files']['name'][$n], strrpos($_FILES['files']['name'][$n],'.'));
if($File_Ext==".png" || $File_Ext==".jpeg" || $File_Ext==".gif" || $File_Ext==".jpg")
{
$srcPath = 'uploads/'; // your DIR name
$path = trim($srcPath.$fileName);
move_uploaded_file($_FILES['files']['tmp_name'][$n], $path)
{
// insertion query here success
}else{
// file not move to the destination
}
}
else
{
//extention not valid
}
$n++;
}
这篇关于上传多个文件pdo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文