2017-03-09 27 views
0

以下是我用來顯示圖像從長數據庫格式的數據庫中的代碼。不能顯示圖像從Mysql試圖這麼多的解決方案似乎沒有任何工作

upload.php的:

if(isset($_FILES['files'])){ 
    $servername = "localhost"; 
    $username = "root"; 
    $password = "123456"; 
    $dbname = "photost"; 
    $conn=mysqli_connect($servername, $username, $password, $dbname); 
    $user_id=$_POST['user_id']; 
    if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
    } 
    $errors=""; 
    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]; 
     $file_content = file_get_contents($file_tmp); 
     $maxsize = 10000000; 

    if($_FILES['files']['tmp_name'][$key]==UPLOAD_ERR_OK) { 
      if(is_uploaded_file($_FILES['files']['tmp_name'][$key])) { 
       if($_FILES['files']['tmp_name'][$key] < $maxsize) { 
        $finfo = finfo_open(FILEINFO_MIME_TYPE); 
        if(strpos(finfo_file($finfo, $_FILES['files']['tmp_name'][$key],"image"))==0) { 
         $imgData = addslashes(file_get_contents($_FILES['files']['tmp_name'][$key])); 
         $query="INSERT INTO `images`(`id`, `u_id`, `image`, `status`, `file_name`, `file_size`) VALUES (NULL,'$user_id','{$imgData}',0,'$file_name','$file_type');"; 
         if (mysqli_query($conn, $query)) { 
          echo "New record created successfully"; 
          } else { 
           echo "Error: <br>" . mysqli_error($conn); 
       } 
       else 
           echo "<p>Uploaded file is not an image.</p>"; 
           } 
           } 
          } 
          } 
          } 



<body> 
    <div style="width:100%;height:40%"> 
    </div> 
<div align=center> 
<form action="" method="POST" enctype="multipart/form-data"> 
    <table style="width:40%"> 
    <tr> 
     <th align="left">User Id:</th> 
     <th align="left"><input type='text' name='user_id'></th> 
    </tr> 
    <tr> 
     <th></th> 
     <th align="left"><input type="file" name="files[]" multiple/></th> 
    </tr> 
    <tr> 
     <th></th> 
     <th ><input type="submit"/></th> 
    </tr> 

    </table> 

    </form> 
</div> 
</body> 

test.php的:

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

getImage.php:

<?php 
$id = $_GET['id']; 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "photost"; 
$conn=mysqli_connect($servername, $username, $password, $dbname); 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 
$query="SELECT * FROM `images` WHERE `id`=$id"; 
$result =mysqli_query($conn, $query); 
$row = mysqli_fetch_assoc($result); 

$image =$row['image']; 

/** check if the image is db */ 
if($image!=null) 
{ 
    $db_img = imagecreatefromstring($image); 
    Header("Content-type: image/jpeg"); 
    imagejpeg($db_img); 

} 
mysql_close($conn); 
?> 

我也試過這樣:

$row = mysql_fetch_assoc($result); 

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

    echo $row['image']; 
    mysql_close($conn); 

即時得到輸出這樣

https://i.stack.imgur.com/RDkdM.png

一件事我可以通過phpMyAdmin的下載它並將其保存爲JPEG格式見圖片。

+0

從數據庫中獲取圖像取決於您如何存儲它,這是第一位。向我們顯示該代碼也是 – RiggsFolly

+0

將圖像存儲在驅動器上並將位置存儲在數據庫中會更好。你也開放給SQL注入。你應該參數化你的查詢......或者至少將'$ id'強制轉換爲int。 – chris85

+0

你可以禁用所有的錯誤報告,使用curl下載一個圖像,並在你得到的結果和原始文件之間做十六進制/字節比較? – Dimi

回答

-1

mysqli_fetch_assoc函數返回每一行的數組。 現在嘗試

$image = $row[0]['image']; 

代替:

$image = $row['image']; 
+0

沒有工作...... –

-1

正確的:

$image=$row['image']; 

的顯示部分應

<?php echo BASE_URL;?>/image_folder/<?php echo $image?> 

意味着你必須創建一個文件夾保存圖片 在你的項目文件中。

我希望這可以幫助你。

+0

im以longblob格式存儲圖像中的數據庫。你可以幫助顯示來自db的圖像..... –

相關問題