2014-02-26 37 views
1

我試圖上傳圖像(blob)到數據庫和使用PHP顯示圖像。要顯示正在使用的所有信息json_encode.Belowü可以看到這裏的輸出PHP代碼上傳圖像到MySQL並顯示在JSON

{"image":[{"image_id":"1","title":"BMW X1","image_name":"Foursquare-icon.png","image":null,"price":"300","description":"test 1"} 

問題是我得到的圖像路徑而不是URL。任何人都可以打電話給我如何獲得圖像url.I想要類似於這個

{"countries":[{"countryname":"India","flag":"http:\/\/wptrafficanalyzer.in\/p\/demo1\/india.png","language":"Hindi","capital":"New Delhi","currency":{"code":"INR","currencyname":"Rupee"}} 

下面是我的PHP代碼來獲取數據。

mysql_connect("localhost","root",""); 
mysql_select_db("database"); 
if(isset($_GET['id'])){ 

    $id = mysql_real_escape_string($_GET['id']); 
    $query = mysql_query("SELECT * FROM `testblob`"); 

    $response_array = array(); 
    $pic_array = array(); 

    while ($row = mysql_fetch_assoc($query)) { 

     $data['image_id'] = $row["image_id"]; 
    $data['title'] = $row["title"]; 

     $data['image_name'] = $row["image_name"]; 
    $data['image_type'] = $row["image_type"]; 
    $data['image'] = $row["image"]; 
    $data['price'] = $row["price"]; 
    $data['image_size'] = $row["image_size"]; 
    $data['image_ctgy'] = $row["image_ctgy"]; 
$data['description'] = $row["description"]; 

     array_push($pic_array, $data); 
    } 

    $response_array = array('image' => $pic_array); 

    echo json_encode($response_array); 

}else{ 
    echo "Error!"; 

}** 

下面是我上傳的PHP文件提前

<?php 

if(!isset($_FILES['userfile'])) 
{ 
echo '<p>Please select a file</p>'; 
} 
else 
{ 
try { 
    upload(); 
    /*** give praise and thanks to the php gods ***/ 
    echo '<p>Thank you for submitting</p>'; 
    } 
catch(Exception $e) 
    { 
    echo '<h4>'.$e->getMessage().'</h4>'; 
    } 
} 

function upload(){ 
/*** check if a file was uploaded ***/ 
if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false) 
{ 
/*** get the image info. ***/ 
$size = getimagesize($_FILES['userfile']['tmp_name']); 
/*** assign our variables ***/ 
$type = $size['mime']; 
$imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb'); 
$size = $size[3]; 
$name = $_FILES['userfile']['name']; 
$maxsize = 99999999; 


/*** check the file is less than the maximum file size ***/ 
if($_FILES['userfile']['size'] < $maxsize) 
    { 
    /*** connect to db ***/ 
    $dbh = new PDO("mysql:host=localhost;dbname=swapmeet", 'root', ''); 

      /*** set the error mode ***/ 
      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     /*** our sql query ***/ 
    $stmt = $dbh->prepare("INSERT INTO testblob (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)"); 

    /*** bind the params ***/ 
    $stmt->bindParam(1, $type); 
    $stmt->bindParam(2, $imgfp, PDO::PARAM_LOB); 
    $stmt->bindParam(3, $size); 
    $stmt->bindParam(4, $name); 

    /*** execute the query ***/ 
    $stmt->execute(); 
    } 
else 
    { 
    /*** throw an exception is image is not of type ***/ 
    throw new Exception("File Size Error"); 
    } 
} 
else 
{ 
// if the file is not less than the maximum allowed, print an error 
throw new Exception("Unsupported Image Format!"); 
} 
} 

?> 

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

感謝。

回答

1

你試圖做的是不會工作。在您的代碼中,您將圖像數據存儲在數據庫表中。返回JSON編碼圖像信息和位置的方法確實包含表格中的圖像數據,因此無法使用JSON發送圖像數據以顯示圖像。您需要更改返回JSON數據的方法,以便它不從表中讀取圖像數據。你需要做的是插入一個條目是你要求爲你的URL問題:

$data['image'] = 'http://some/url/to/getimage.php?id=1' 

然後,你需要另一個PHP腳本,從數據庫返回的圖像,當上面的網址是運行調用。 PHP腳本將查詢數據庫並返回一個HTTP響應,併爲圖像類型提供適當的Content-Type標頭。舉例來看How to retrieve images from MySQL database and display in an html tag

的回答