2015-02-06 47 views
0

我無法讓我的大屏幕顯示數據庫中的圖像(以BLOB形式存儲)。我試過 標題(「Content-type:image/jpeg」); echo'';無法使用PHP顯示數據庫中的斑點圖像

但它仍然無法顯示在瀏覽器上。

getImage.php

<?php 
require_once 'php-activerecord/ActiveRecord.php'; 

ActiveRecord\Config::initialize(function($cfg) { 
    $cfg->set_model_directory('models'); 
    $cfg->set_connections(array(
     'development' => 'mysql://root:[email protected]/BondingTogether')); 
}); 

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

$row = food::find_by_foodid($id); 
$image = $row->image; 
//$image = "" 


//header("Content-Type: image/jpg"); 
header("Content-type: image/jpeg"); 
//echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>'; 
echo $image; 
//echo base64_decode($image); 

添加到數據庫

<?php 

require_once 'php-activerecord/ActiveRecord.php'; 

ActiveRecord\Config::initialize(function($cfg) { 
    $cfg->set_model_directory('models'); 
    $cfg->set_connections(array(
     'development' => 'mysql://root:[email protected]/BondingTogether')); 
}); 

//files 
$file = $_FILES['foodimage']['tmp_name']; 
if (!isset($file)) { 
} 
else { 
    //$image = addslashes(file_get_contents($_FILES['foodimage']['tmp_name'])); 
    $image_name = addslashes($_FILES['foodimage']['tmp_name']); 
    $image_size = getimagesize($_FILES['foodimage']['tmp_name']); 

    if ($image_size == FALSE) { 
     die('Please select an image file'); 
    } else { 

    } 
} 
$image = addslashes(file_get_contents($_FILES['foodimage']['tmp_name'])); 
//$image = chunk_split(base64_encode(file_get_contents("image.jpg"))); 

Food::create(array(
    'xcoord' => $_POST['XCoord'] 
    , 'ycoord' => $_POST['YCoord'] 
    , 'title' => $_POST['title'] 
    , 'category' => $_POST['cat'] 
    , 'description' => $_POST['desc'] 
    , 'image' => $image 
)); 
+0

我試過頭(「Content-type:image/jpeg」);並回顯''; – 2015-02-06 03:44:50

回答

2

你不應該使用和addslashes被()在你的數據庫存儲圖像數據時。更好的選擇是在輸出圖像數據時使用base64_encode()和base64_decode()。一個簡單的搜索將找到很多這個和類似的問題很好的答案

+0

所以它是addslashes!非常感謝你! – 2015-02-06 07:46:42