Php 插入表单并多次上传
我想插一则广告。广告包含一个标题、一些文本、一个价格,可以有一个或多个图像。表格都在一页上。php和html在一个文件中也是如此 在我的数据库中,我有一个Php 插入表单并多次上传,php,html,mysql,upload,Php,Html,Mysql,Upload,我想插一则广告。广告包含一个标题、一些文本、一个价格,可以有一个或多个图像。表格都在一页上。php和html在一个文件中也是如此 在我的数据库中,我有一个广告表和一个图像表。它们都有一个唯一的id作为主id 图像表还包含一个文件名和一个来自广告表的广告id,它是该表的外键 问题: 如何上载多个文件 我需要一次插入所有内容,还是先插入广告,然后再插入图片 图片: 至于你的第二个问题,我认为插入广告比插入所有图片更明智 第一个问题, html代码应该是这样的 <form name="
广告
表和一个图像
表。它们都有一个唯一的id作为主id
图像
表还包含一个文件名
和一个来自广告表
的广告id
,它是该表的外键
问题:
图片:
至于你的第二个问题,我认为插入广告比插入所有图片更明智
第一个问题,
html代码应该是这样的
<form name="multiupload" action="uploadmany.php" method="post" enctype="multipart/form-data">
<input name="filesToUpload[]" type="file" multiple /></td>
<td collapse=2><input name="savemultiimg" type="submit" value="submit"></td>
</form>
这段代码来自我的一个大学项目,我刚刚对它进行了修改以向您展示。如果这给你带来了任何错误,告诉我,我会很乐意帮助你
源代码已从中复制,并对多次上载进行了更改。我现在将尝试,我会让您知道结果!我无法检查它是否工作。该页面没有显示任何与该页面中实现的代码有关的内容。由于我的错误,在进行了一些更改后,它工作得非常好!我只需要一个多选择器。你是说类似的东西吗?是的,但我尝试了其中一些,但无法正确安装:(
<form name="multiupload" action="uploadmany.php" method="post" enctype="multipart/form-data">
<input name="filesToUpload[]" type="file" multiple /></td>
<td collapse=2><input name="savemultiimg" type="submit" value="submit"></td>
</form>
<?php
require("DataBaseConnection.php");
if(isset($_POST["savemultiimg"])) {
for($i=0;$i<count($_FILES['filesToUpload']['tmp_name']);$i++){
$name=$_FILES['filesToUpload']['name'][$i];
$tmp_name=$_FILES['filesToUpload']['tmp_name'][$i];
$target_dir = "images/"; //the dir where you want to save images
$target_file = $target_dir . rand().'_'.($name); //this will be the name of the img, and we will give it a random number so if you uploaded two image with same name you don't have a problem
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
$check = getimagesize($tmp_name);
if($check !== false) {
//echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
$err= "file you uploaded is not an image";
$uploadOk = 0;
}//end if
// Check if file already exists,, this will appear if you removed the random number on the top
if (file_exists($target_file)) {
$err ="file already exists";
$uploadOk = 0;
}//end if
if ($uploadOk != 0)
{
if (move_uploaded_file($tmp_name, $target_file)) {
$err ="file uploaded succ.";
$sql = "...";//your sql here
$result = $con->query($sql);
} else {
$err ="unecpected error occured";
} //end if upload
}//end ($uploadOk != 0)
}//end forloop
header('Location: index.php?err='.$err.');
}
?>