插入表单和多次上传
Insert form and multiple upload
我要插入广告。广告包含标题、一些文本、价格,并且可以有一个或多个图像。表格都在一页上。一个文件中的 php 和 html 也是如此。
在我的数据库中,我有一个 advertisement
table 和一个 image
table。他们都有一个唯一的 id 作为主要。
image
table 还包含 filename
和来自 advertisement table
的 advertisement_id
是 table 的外键.
问题:
如何上传多个文件?
是一次性全部插入还是先插入广告再插入图片?
<div class="form-group">
<label for="image" class="col-sm-2 control-label">Image:</label>
<div class="col-sm-6">
<input type="file" id="exampleInputFile" name="filename[]" id="filename" multiple">
</div>
</div>
关于你的第二个问题,我觉得先插广告再插图片比较明智
对于你的第一个问题,
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>
这里是 php 代码。变量$err
是上传状态的通知
<?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.');
}
?>
此代码来自我的一个大学项目,我刚刚对其进行了修改以满足您的需求。如果它给你带来任何错误,请告诉我,我很乐意帮助你。
源代码复制自w3schools,并针对多次上传进行了更改。
我要插入广告。广告包含标题、一些文本、价格,并且可以有一个或多个图像。表格都在一页上。一个文件中的 php 和 html 也是如此。
在我的数据库中,我有一个 advertisement
table 和一个 image
table。他们都有一个唯一的 id 作为主要。
image
table 还包含 filename
和来自 advertisement table
的 advertisement_id
是 table 的外键.
问题:
如何上传多个文件?
是一次性全部插入还是先插入广告再插入图片?
<div class="form-group">
<label for="image" class="col-sm-2 control-label">Image:</label>
<div class="col-sm-6">
<input type="file" id="exampleInputFile" name="filename[]" id="filename" multiple">
</div>
</div>
关于你的第二个问题,我觉得先插广告再插图片比较明智
对于你的第一个问题,
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>
这里是 php 代码。变量$err
是上传状态的通知
<?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.');
}
?>
此代码来自我的一个大学项目,我刚刚对其进行了修改以满足您的需求。如果它给你带来任何错误,请告诉我,我很乐意帮助你。
源代码复制自w3schools,并针对多次上传进行了更改。