我试图将图像(blob)上传到数据库并使用php显示图像。使用json_encode显示所有信息。你可以看到输出
{"image":[{"image_id":"1","title":"BMW X1","image_name":"Foursquare-icon.png","image":null,"price":"300","description":"test 1"}
这里的问题是我的图片路径不是网址。任何人都可以打电话给我如何获取图像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>
提前致谢。
答案 0 :(得分:1)
你想做的事情是行不通的。在代码中,您将图像数据存储在数据库表中。您返回JSON编码图像信息和位置的方法确实包含来自表的图像数据,您无法以JSON格式发送图像数据以显示图像。您需要更改返回JSON数据的方法,以便它不会从表中读取图像数据。您需要做的是插入一个条目,该条目是您在问题中要求的URL:
$data['image'] = 'http://some/url/to/getimage.php?id=1'
然后,您需要另一个PHP脚本,该脚本从数据库返回图像,并在调用上述URL时运行。 PHP脚本将查询数据库并返回HTTP响应,其中包含适用于图像类型的Content-Type标头。有关示例,请查看How to retrieve images from MySQL database and display in an html tag
的答案