php – 上传多个图像并将其路径存储在数据库中

前端之家收集整理的这篇文章主要介绍了php – 上传多个图像并将其路径存储在数据库中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在制作一个表单,用户可以通过该表单上传多个图像.当用户上传图像时,它们会存储在服务器的文件夹中.一切正常,直到这里,但是当我试图保存数据库中的图像路径而不是路径时,两个图像的名称只存储在一行中.我希望每个图像的路径应该存储在不同的行中.
<form action="admin_insert_property_images.PHP" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label class="col-md-3 control-label">Upload Image:</label>
            <div class="col-md-8">
                <input type="file" id="file" name="support_images[]" multiple accept="image/*" />
            </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label"></label>
            <div class="submit">
                <input class="btn btn-primary" value="Save " type="submit" name="submit">
            </div>  
    </div>
</form>

admin_insert_property_images.PHP

<?PHP
$con=MysqLi_connect("abc.com","abc","ab","abc");
// Check connection
if (MysqLi_connect_errno()) 
    {
        echo "Failed to connect to MysqL: " . MysqLi_connect_error();
    }

if(isset($_POST['submit']))           
{
 extract($_POST);

    if(isset($_FILES['support_images']['name']))
    {
        $file_name_all="";
        for($i=0; $i<count($_FILES['support_images']['name']); $i++) 
        {
               $tmpFilePath = $_FILES['support_images']['tmp_name'][$i];    
               if ($tmpFilePath != "")
               {    
                   $path = "propertyimages/"; // create folder 
                   $name = $_FILES['support_images']['name'][$i];
                  $size = $_FILES['support_images']['size'][$i];

                   list($txt,$ext) = explode(".",$name);
                   $file= time().substr(str_replace(" ","_",$txt),0);
                   $info = pathinfo($file);
                   $filename = $file.".".$ext;
                   if(move_uploaded_file($_FILES['support_images']['tmp_name'][$i],$path.$filename)) 
                   { 
                      $file_name_all.=$filename."*";
                   }
             }
        }
        $filepath = rtrim($file_name_all,'*'); 
$query=MysqLi_query($con,"INSERT into propertyimages (`propertyimage`) VALUES('".addslashes($filepath)."'); ");    
        }
        else
    {
        $filepath="";
    }

    if($query)
    {
       header("Location: admin_profile.PHP");
    }
}
?>
你的$filepath变量和你的查询必须在你的循环中.

您还使用与MysqLi_ functions不兼容的MysqL_query.

这两个API不能混合在一起.将DB连接传递给它时使用MysqLi_query.

<?PHP
$con=MysqLi_connect("abc.com",$path.$filename)) 
                   { 
                      $file_name_all.=$filename."*";
                   }
             }
              $filepath = rtrim($file_name_all,'*').$path;    
         $query=MysqLi_query($con,"INSERT into propertyimages (`propertyimage`) VALUES('".addslashes($filepath)."'); ");
        }

    }
    else
    {
        $filepath="";
    }

    if($query)
    {
       header("Location: admin_profile.PHP");
    }
}
原文链接:https://www.f2er.com/php/138334.html

猜你在找的PHP相关文章