显示数据库中的所有图像(php)

时间:2014-12-09 16:54:17

标签: php database image upload

我使用此代码在数据库中上传图片:

<?php

//Store the upload form
$UploadForm = " <form id='idForm' action='upload.php' method='post' enctype='multipart/form-data'>
                    <input type='file' name='image'/><br/><br/>
                    <input id='BTN' type='submit' value='Upload'/><br/><br/>
            </form>";
//if logged in show the upload form
if($userid && $username){
echo $UploadForm;
// Connect to database
$con = mysqli_connect('***', '***', '***', '***_dbimage');
// Check connection
if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//file properties
if(isset($_FILES['image'])){
    $file = $_FILES['image']['tmp_name'];
}

//if image selected
if(isset($file) && $file != ""){
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

if($image_size == FALSE){
    echo "That's not an image!";
    header( "refresh:2;url=upload.php" );
}
else{
    $qry = mysqli_query($con,"SELECT * FROM store WHERE name='$image_name'");
    $Nrows = $qry->num_rows;
    if( $Nrows == 0){
            if(!$insert = mysqli_query($con,"INSERT INTO store VALUES         ('','$image_name','$username','$image')")){
            echo "We had problems uploading your file!";
            header( "refresh:2;url=upload.php" );
        }
        else{
            echo "Image $image_name uploaded!";
            header( "refresh:2;url=upload.php" );
        }
    }
    else{
        echo "There is already an image uploaded with the name $image_name<br/>";
    }
}
}   
else{
    echo "Please select an image";
}
mysqli_close($con);
}
else{
    echo "You have to be logged in to upload!";
}
?>

此代码显示数据库中的所有图像:

// Connect to database
$con = mysqli_connect('***', '***', '***', '***_dbimage');
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$res = mysqli_query($con,'SELECT * FROM store');
while($row = $res->fetch_array()){
    $image = $rows['image'];
    echo "<img src='".$image."' />";
}

我得到像钻石一样的大量随机符号,里面有问号和字母而不是我的图像。这些脚本不是我制作的。我只是看了一些教程并将它们结合起来,似乎我没有&#34;组合&#34;他们正确。我做错了什么?

LATER EDIT:

HTML:

<img src="getImage.php?id=26"/>

PHP(getImage.php):     

$con = mysqli_connect('***', '***', '***', '***_dbimage');
if(isset($_GET['id']))
{
    $id = mysql_real_escape_string($_GET['id']);
    $query = mysql_query("SELECT * FROM store WHERE id=$id");
    while($row = mysql_fetch_assoc($query))
    {
        $imageData = $row['image'];
    }
    header("content-type:image/jpeg");
    echo $imageData;
}
else
{
    echo "Error!";
}

?>

仍然无法让它发挥作用!求救!

1 个答案:

答案 0 :(得分:1)

我最终做到了! 这是上传脚本:

<?php

//Store the upload form
$UploadForm = " <form id='idForm' action='upload.php' method='post' enctype='multipart/form-data'>
                    <input type='file' name='image'/><br/><br/>
                    <input id='BTN' type='submit' value='Upload'/><br/><br/>
            </form>";
//if logged in show the upload form
if($userid && $username){
    echo $UploadForm;
// Connect to database
$con = mysqli_connect('***', '***', '***', '***_dbimage');
// Check connection
if (mysqli_connect_errno())
  {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

//file properties
if(isset($_FILES['image'])){
    $file = $_FILES['image']['tmp_name'];
}

//if image selected
if(isset($file) && $file != ""){
    $image = mysqli_real_escape_string($con,file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if($image_size == FALSE){
        echo "That's not an image!";
        header( "refresh:2;url=upload.php" );
    }
    else{
        $qry = mysqli_query($con,"SELECT * FROM store WHERE name='$image_name'");
        $Nrows = $qry->num_rows;
        if( $Nrows == 0){
            if(!$insert = mysqli_query($con,"INSERT INTO store VALUES     ('','$image_name','$username','$image')")){
            echo "We had problems uploading your file!";
            header( "refresh:2;url=upload.php" );
        }
        else{
            echo "Image $image_name uploaded!";
            header( "refresh:2;url=upload.php" );
        }
    }
    else{
        echo "There is already an image uploaded with the name $image_name<br/>";
    }
}
}   
else{
    echo "Please select an image";
}
mysqli_close($con);
}
else{
    echo "You have to be logged in to upload!";
}
?>

以下是diplay脚本:

$con = mysqli_connect('***', '***', '***', '***_dbimage');
$query = mysqli_query($con,"SELECT id FROM store");
while($row = mysqli_fetch_assoc($query))
{
    $IDstore = $row['id'];
    echo "<img src='getImage.php?id=".$IDstore."'/>";
}

和“getImage.php”:

<?php

$con = mysqli_connect('***', '***', '***', '***_dbimage');
if(isset($_GET['id']))
{
    $id = mysqli_real_escape_string($con,$_GET['id']);
    $query = mysqli_query($con,"SELECT * FROM store WHERE id=$id");
    while($row = mysqli_fetch_assoc($query))
    {
        $imageData = $row['image'];
    }
    header("content-type:image/jpeg");
    echo $imageData;
}
else
{
    echo "Error!";
}

?>

我希望它会帮助某人让它现在可以使用了。 :)