gpt4 book ai didi

php - 将多个图像上传到每行 PHP 和 Mysqli 数据库

转载 作者:行者123 更新时间:2023-11-28 23:27:28 26 4
gpt4 key购买 nike

我有 3 张图片要上传到我的表单中。

    <form action="" method="post" enctype="multipart/form-data" class="form-horizontal">
<div class="form-group col-md-5">
<label for="image">Centralizada</label>
<input id="image" type="file" name="image" class="btn btn-danger">
</div>

<div class="form-group col-md-5">
<label for="img_v1">V1</label>
<input id="img_v1" type="file" name="img_v1" class="btn btn-danger">
</div>

<div class="form-group col-md-5">
<label for="img_v2">V2</label>
<input id="img_v2" type="file" name="img_v2" class="btn btn-danger">
</div>.

我的数据库中有这三行:imageimg_v1img_v2

我想将所有 3 张图像上传到每一行,但我的脚本无法正常工作。它只发布第一张图片,而其他两张则没有。

这是我的脚本

<?php 
include("includes/dbconn.php");


$error = '';
if(isset($_POST['submit_post'])){
$title = strip_tags($_POST['title']);
$date = date('Y-m-d h:i:s');
if($_FILES['image']['name'] !=''){
$image_name = $_FILES['image']['name'];
$image_tmp = $_FILES['image']['tmp_name'];
$image_size = $_FILES['image']['size'];
$image_ext = pathinfo($image_name,PATHINFO_EXTENSION);
$image_path = '../clientes/img/'.$image_name;
$image_db_path = 'img/'.$image_name;
if($image_size < 10000000){
if($image_ext == 'jpg' || $image_ext == 'png' || $image_ext == 'jpeg' || $image_ext == 'gif'){
if(move_uploaded_file($image_tmp,$image_path)){
$ins_sql = "INSERT INTO gallery (title, description, image, img_v1, img_v2, category, status) VALUES ('$title', '$_POST[description]',
'$image_db_path', '$image_db_path', '$image_db_path', '$_POST[category]', '$_POST[status]')";

为什么我的其他图片现在正在上传?Obs:我正在学习 php -noob

最佳答案

试试这个(我没有测试过)

function imageUpload($field)
{
$image_name = $_FILES[$field]['name'];
$image_tmp = $_FILES[$field]['tmp_name'];
$image_size = $_FILES[$field]['size'];
$image_ext = pathinfo($image_name, PATHINFO_EXTENSION);
$image_path = '../clientes/img/' . $image_name;
if ($image_size < 10000000) {
if ($image_ext == 'jpg' || $image_ext == 'png' || $image_ext == 'jpeg' || $image_ext == 'gif') {
return move_uploaded_file($image_tmp, $image_path);
}
}
return false;
}



if (isset($_POST['submit_post'])) {
$title = strip_tags($_POST['title']);
$date = date('Y-m-d h:i:s');
if ($_FILES['image']['name'] != '') {

$upload = imageUpload('image');
$image_db_path = 'img/' . $_FILES['image']['name'];
$upload1 = imageUpload('img_v1');
$image_db_path1 = 'img/' . $_FILES['img_v1']['name'];
$upload2 = imageUpload('img_v2');
$image_db_path2 = 'img/' . $_FILES['img_v2']['name'];

if ($upload && $upload1 && $upload2)
$ins_sql = "INSERT INTO gallery (title, description, image, img_v1, img_v2, category, status) VALUES ('$title', '$_POST[description]',
'$image_db_path', '$image_db_path1', '$image_db_path2', '$_POST[category]', '$_POST[status]')";

}
}

关于php - 将多个图像上传到每行 PHP 和 Mysqli 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703884/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com