2016-06-28 60 views
0

好吧,所以我上传的图片代码如下,但我不知道如何显示它,我想让画廊,所以我想要的是显示在一个页面上的所有图像,如果你可以解释这也会有帮助!如何显示数据库的所有斑点(图片)

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

?>

此代码是获得图像,但只显示最后一个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; 

?>

+0

您发布了上载图像的代码。你到目前为止试过什么来展示它? –

+0

将图像保存为斑点不是一种好的做法。将url保存到数据库中的图像上,然后将图像作为文件保存到某个文件夹中。然后你所要检索的是图像url到文件 – theTypan

+0

我怎么能保存从文件夹到网址的URL你能告诉我的例子吗? – Praziluk

回答

0

保存图像作为文件而不是blob。商店网址到图像数据库为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; 

要获得从数据库的图像,你可以有从表格图像中选择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*/?> 
+0

但它不插入它的数据库?在你的代码中,我应该在db中插入什么?也在那个查询中,我应该从图像中选择所有来显示它? – Praziluk

+0

因为我假定你的'image_id'是一个自动增量,所以你应该在$ db中插入'$ image'。 'mysqli_query($ connect,“INSERT INTO images('image')VALUES('$ image')”)'在查询中,你只能选择'image',因为它只有你需要的信息。 – theTypan

+0

我已经修改了答案,用你可以使用的工具代替'insert_image'函数 – theTypan

相关问题