如何从多图像上传显示多个图像

时间:2016-01-11 17:34:34

标签: php mysql image upload display

我使用以下代码通过我的网站的管理区域将多个图像添加到我的数据库,这很好用,但我想显示为特定车辆ID上传的多个图像...基本上我是试图为汽车经销商创建一个网站,当管理员添加股票时,他们可以选择多个图像添加到列表中,这里是添加股票代码

<?php

if(isset($_POST['addstock'])){

$vehicle_make = $_POST['vehicle_make'];
$veh_model = $_POST['veh_model'];
$veh_mileage = $_POST['veh_mileage'];
$veh_description = $_POST['veh_description'];
$veh_price = $_POST['veh_price'];
$veh_gearbox = $_POST['veh_gearbox'];
$veh_engine_size = $_POST['veh_engine_size'];
$veh_fuel_type = $_POST['veh_fuel_type'];

foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){

$file_name = $key.$_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];


$addstock = "insert into stock (veh_make,veh_model,veh_mileage,veh_description,veh_gearbox,veh_engine_size,veh_fuel_type,veh_price,file_name,file_size,file_type) values('$vehicle_makee','$veh_model','$veh_mileage','$veh_description','$veh_gearbox','$veh_engine_size','$veh_fuel_type','$veh_price','$file_name','$file_size','$file_type')";

        move_uploaded_file($file_tmp,"image_uploads/".$file_name);
        }

$addsto = mysqli_query($con, $addstock);

if($addsto){

    echo "<script>alert('Vehicle has been added')</script>";
    echo "<script>window.open('addstock.php','_self')</script>";
}
}

代码成功地将多个图像添加到目录文件夹

以下是管理区域外的代码,在车辆详细信息页面上,我上传的多张图片中只显示了1张图片,我希望显示为车辆ID上传的所有图片,最终将进行样式设置进入图库,

<?php
if(isset($_GET['vehicle_id'])){

$vehicle_id = $_GET['vehicle_id'];

$get_veh = "select * from stock where vehicle_id='$vehicle_id'";

$run_veh = mysqli_query($con, $get_veh);

while($row_veh=mysqli_fetch_array($run_veh)){

    $vehicle_id = $row_veh['vehicle_id'];
    $veh_make = $row_veh['veh_make'];
    $veh_model = $row_veh['veh_model'];
    $veh_mileage = $row_veh['veh_mileage'];
    $veh_price = $row_veh['veh_price'];
    $veh_gearbox = $row_veh['veh_gearbox'];
    $veh_description = $row_veh['veh_description'];
    $file_name = $row_veh['file_name'];

    echo "

        <div id='single_vehicle'>


            <div id='box1'>$veh_make $veh_model</div>
            <div id='box3'>£$veh_price</div>
        </div>


        <div id='single_vehicle2'>

            <div id='box2'><img src='admin/image_uploads/$file_name' width='600' height='450' /></div>
            <div id='box4'>
            <div id='clickbox1'><a href='#'>Book Test Drive</a></div>
            <div id='clickbox1'><a href='#'>Send Enquiery</a></div>
            <div id='clickbox1'><a href='#'>Print this Page</a></div>
            <div id='clickbox1'><a href='#'>Email this Page</a></div>

            </div>



        </div>

        <div id='desription_area'>
        <div id='desription_area1'><span style='text-decoration:underline'>Vehicle Description</span><br>$veh_description</div>
        <div id='desription_area2'><span style='text-decoration:underline'>Specification</span><br>Make: $veh_make<br>$veh_model<br>$veh_mileage</div>
        </div>

        ";
}
}
?>

我是编码和学习的新手,我做错了什么? 非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

嗨,也许您的代码有错误,请尝试并检查您的代码

上传图片

<?php
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "root", "");
mysql_select_db ("phppot_examples");
$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));
$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);
$sql = "INSERT INTO output_images(imageType ,imageData)
VALUES('{$imageProperties['mime']}', '{$imgData}')";
$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: listImages.php");
}}}
?>
<HTML>
<HEAD>
<TITLE>Upload Image to MySQL BLOB</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
</div>
</BODY>
</HTML>

并显示图像保存在数据库

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("phppot_examples") or die(mysql_error());
if(isset($_GET['image_id'])) {
$sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];
$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
$row = mysql_fetch_array($result);
header("Content-type: " . $row["imageType"]);
echo $row["imageData"];
}
mysql_close($conn);
?>

列表图像

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("phppot_examples");
$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC"; 
$result = mysql_query($sql);
?>
<HTML>
<HEAD>
<TITLE>List BLOB Images</TITLE>
<link href="imageStyles.css" rel="stylesheet" type="text/css" />
</HEAD>
<BODY>
<?php
while($row = mysql_fetch_array($result)) {
?>
<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/>
<?php       
}
mysql_close($conn);
?>
</BODY>
</HTML>

为了向浏览器显示BLOB图像,我们必须创建一个PHP文件来执行以下操作。

从MySQL BLOB获取图像数据和类型使用PHP header()将内容类型设置为图像(image / jpg,image / gif,...)。打印图像内容。

祝你好运并尝试