我遇到一个问题,我一次只能上传1张图片。
我希望它选择多个图像并上传并存储到MySQL数据库。
有人可以帮助我吗?
这是我上传图片的表单。
form.php的
<form method="post" action="" enctype="multipart/form-data">
<div class="col-md-6">
<div class="w3-card-4 w3-white" style="padding:10px;">
<div class="w3-container w3-padding">
<h6 class="w3-opacity">Hello, <b><?php echo $row['firstname'] ?>!</b></h6>
<p align="center">
<textarea class="form-control" onclick="this.value='';" placeholder="What's on your mind?" name="textarea" style="height:4em;" id="text"></textarea>
</p>
<div class="pull-right">
<img src="" id="imagePreview" width="100" >
<input type="file" name="image" id="imageUpload" class="hide"/>
<label for="imageUpload"><font size="5"><i class="fa fa-camera" aria-hidden="true"></i></font></label>
<button type="submit" id="share" name="share" class="w3-btn w3-green"><i class="fa fa-pencil"></i> Post</button>
</div>
</div>
</div>
</div>
</form>
这是我的
upload.php的
<?php
session_start();
include("connection.php");
include("function.php");
if($_SESSION['login'] != 'true'){
header("location:index.php");
}
$id = $_SESSION['member_id'];
// if(isset($_POST['share'])){
// $post = $_POST['textarea'];
// mysql_query("INSERT INTO posts(member_id,actualpost,date,post_to) VALUE ('$id','$post',NOW(),'$id')")or die(mysql_error());
// header("location: hometest.php");
// }
if(isset($_POST['comment'])){
$comment = $_POST['textarea'];
$postid = $_POST['ucantseeme'];
mysql_query("INSERT INTO postcomment(postid,memberid,date,comment) VALUE ('$postid','$id',NOW(),'$comment')")or die(mysql_error());
}
if( isset($_POST['textarea']) && !empty($_POST['textarea']) ){
$content=$_POST['textarea'];
}else{
$content="";
}
**// THIS IS WHERE I UPLOAD THE IMAGE**
if ( isset($_FILES['image']['tmp_name']) && !empty($_FILES['image']['tmp_name']) ) {
$file=$_FILES['image']['tmp_name'];
$image = $_FILES["image"] ["name"];
$image_name= addslashes($_FILES['image']['name']);
$size = $_FILES["image"] ["size"];
$error = $_FILES["image"] ["error"];
if ($error > 0){
die("Error uploading file! Code $error.");
}
if($size > 10000000){ //conditions for the file
die("Format is not allowed or file size is too big!");
}
move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);
$location="upload/" . $_FILES["image"]["name"];
}else{
$location="";
}
// Save to database if there is content OR an image
// Will save both if both are present
if( !empty($content) || !empty($location) ){
$update=mysql_query(" INSERT INTO posts (member_id,post_image,actualpost,date)
VALUES ('$id','$location','$content',NOW()) ") or die (mySQL_error());
// Redirect to home
header('location:hometest.php');
}
?>