我正在尝试从html表单中获取一个图像文件,以便将其插入数据库中。遗憾的是,由于该文件未发布,因此无法正常工作。
所有其他变量都在工作,我回应他们检查,但文件不是。我的问题在哪里?我认为它是在html表单中,插入的变量也是以相同的形式但在不同的部分,所以帖子实际上将表单中的所有数据发送到post.php而不是图像文件。
如您所见,我已经包含了“enctype =”multipart / form-data“”。
请帮忙吗?
1)HTML标记:
<form action="post.php" method="post" enctype="multipart/form-data">
<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="300000"/>
<div>
<input type="file" id="fileselect" name="fileselect[]" multiple accept="image/*"/>
<div id="filedrag">or drop files here</div>
</div>
<div id="submitbutton">
<button type="submit">Upload Files</button>
</div>
</form>
2)PHP代码:
$title=isset($_POST['title'])?$_POST['title']:false;
$desc=isset($_POST['description'])?$_POST['description']:false;
$date=date("Y-M-D");
$city=isset($_POST['city'])?$_POST['city']:false;
$user=isset($_POST['name'])?$_POST['name']:false;
$price=isset($_POST['price'])?$_POST['price']:false;
$table=isset($_POST['option'])?$_POST['option']:false;
$imgfile=isset($_FILE['file']['name'])?$_FILE['fileselect']['name']:false;
$tmp_dir=isset($_FILE['fileselect']['tmp_name'])?$_FILE['fileselect'['tmp_name']:false;
$img_size=isset($_FILE['fileselect']['size'])?$_FILE['fileselect']['size']:false;
$upload_dir='images/'; // directory
$imgExt=strtolower(pathinfo($imgfile,PATHINFO_EXTENSION)); // GET IMAGE EXTENSION
$valid_ext=array('jpeg','jpg','png','gif');
//rename uploading image
$item_img=rand(1000,1000000).".".$imgExt;
// allow valid image format
if(in_array($imgExt, $valid_ext))
{
if($img_size < 500000)
{
move_uploaded_file($tmp_dir,$upload_dir.$item_img);
}
else{
$erMsg="Sorry , your file is too large";
}
}
else{
$erMsg="Sorry,only JPG,JPEG,PNG & GIF files are accepted :)";
}
$query = $pdo->prepare('INSERT INTO '.$table .'(title,description,image,date,city,user,price) VALUES(:title, :description, :image, :date, :city, :user, :price)');
$query->bindParam(':title', $title);
$query->bindParam(':description', $desc);
$query->bindParam(':image', $item_img);
$query->bindParam(':date', $date);
$query->bindParam(':city', $city);
$query->bindParam(':user', $user);
$query->bindParam(':price', $price);
答案 0 :(得分:2)
要从表单发送文件,它应该具有正确的enctype
属性。
<form action="script.php" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit">
</form>
答案 1 :(得分:1)
如果您正在使用图片等文件,请使用<form enctype="multipart/form-data">
答案 2 :(得分:0)
你看到你在线上有拼写错误吗?
$tmp_dir=isset($_FILE['fileselect'['tmp_name'])$_FILE['fileselect'['tmp_name']:false;
]
'
fileselect
检查!!