dougu2240 2016-06-28 10:46
浏览 156
已采纳

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

Ok so i uploaded images with code below but i dont know how to display it, i want to make gallery so all i want is to display all images on one page, if you could explain that would be helpfull too!

<?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>";
        }
    }
}

?>

This code is to get image but it only display last ID and i dont know how to make it display all images

<?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;

?>

  • 写回答

1条回答 默认 最新

  • dtr53557 2016-06-28 11:10
    关注

    Save images as files instead of blobs. Store url to image in db as varchar

    The 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/');
    

    This is the constant for the path of the images. In this case its a folder named images in the same directory as the script you are working on

    $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;
    

    Your images table could appear like this

      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;
    

    To get an image from db, you could have a query that selects image from table images. To display image in html do

      $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*/?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助