如何显示所有blob(图像)表单数据库

时间:2016-06-28 10:46:49

标签: php

好吧所以我上传了带有以下代码的图片,但我不知道如何显示它,我想制作图库所以我想要的是在一个页面上显示所有图像,如果你能解释那也会有帮助!

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phplogin";

$connect = mysqli_connect($servername,$username,$password,$dbname);

$file = $_FILES['image']['tmp_name'];
$ime = $_POST['ime'];


if(!isset($file))
{
    echo "Izaberite sliku";
}
else
{
    $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 "Niste izabrali dobru sliku";
    }
    else
    {
        if(!$insert = mysqli_query($connect,"INSERT INTO store VALUES ('','$image_name','$image','$ime')"))
        {
            echo "Problem sa postavljanjem slike";
        }
        else
        {
            //$lastid = mysqli_insert_id($connect);
            // I WANT TO DISPLAY IMAGES HERE
            //echo "Image uploaded.<p />Slika:<p /><img src=get.php>";
        }
    }
}

&GT;

此代码是为了获取图像,但它只显示最后一个ID,我不知道如何让它显示所有图像

<?php
mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("databaseimage") or die(mysql_error());

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM store WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpeg");

echo $image;

&GT;

1 个答案:

答案 0 :(得分:0)

将图像保存为文件而不是blob。将url存储到db中的映像作为varchar

html

<form role="form" class="form-inline" enctype="multipart/form-data" method="post" action=""> <div class="form-group"> <input type="file" class="form-control" name="image"/> </div>
<input type="submit" name="upload" class="btn btn-success" value="Upload" /> </form>

PHP

 define('UPLOAD_PATH','images/');

这是图像路径的常量。在这种情况下,它的文件夹名为images,与您正在处理的脚本位于同一目录中

$errors = array();


 function output_errors($errors){
   $output=array();

  foreach($errors as $error){

  $output[]='<li>'.$error.'</li>';
  }
     return  '<ul>'.implode('',$output).'</ul>';

  }

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

    if(!empty($_FILES['image']['name'])){

        $image=$_FILES['image']['name'];
        $image_type=$_FILES['image']['type'];
        $image_size=$_FILES['image']['size'];

         if((($image_type=='image/gif') || ($image_type=='image/jpeg') || ($image_type=='image/png') || ($image_type=='image/pjpeg')) &&
         ($image_size>0) /*&& ($image_size<=MAX_FILE_SIZE)*/){             

           if($_FILES['image']['error']==0){

                 /*give each image a unique name*/
                  $image=microtime().$image;

                 /*move uploaded file to permanent folder*/         
                 $target=UPLOAD_PATH.$image;

                 if(move_uploaded_file($_FILES['image']['tmp_name'],$target)){

                      if(mysqli_query($connect,"INSERT INTO images(`image`) VALUES ('$image')") ){

                        $message="Image was uploaded sucessfully";
                      }else{
                       $errors[]="Error,image was not uploaded successfully";
                       /*delete permanent file from server*/
                       @unlink(UPLOAD_PATH.$image);
                      }                     

                 }/*end of move uploaded file*/
            } 


         }else{
            $errors[]="File uploaded must be of type png, jpeg or gif";

            /*delete temporary image file*/
            @unlink($_FILES['image']['tmp_name']);
         }/*end of image validation*/

    }else{
       $errors[]="Please select an image file";

    }/*empty*/

}





  if(!empty($errors))echo output_errors($errors);

  if(!empty($message)) echo $message;

您的图片表格可能会显示为

  CREATE TABLE IF NOT EXISTS `images` (
  `image_id` int(11) NOT NULL,
  `image` varchar(255) NOT NULL
   ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;

要从db获取图像,您可以使用从表格图像中选择image的查询。在html中显示图像

  $queryImage=mysqli_query($connect,"SELECT `image` FROM images");

    while($rowImage=mysqli_fetch_assoc($queryImage)){?>            

        <img src="<?php echo UPLOAD_PATH.$rowImage['image'] ;?>" style="width:250px; height:200px"/>    

    <?php
    }/*end of while loop getting images*/?>