Am trying to upload image(blob) to database and display image using php. To display all the information am using json_encode.Below u can see the output
{"image":[{"image_id":"1","title":"BMW X1","image_name":"Foursquare-icon.png","image":null,"price":"300","description":"test 1"}
The problem here is am getting the image path not the url. can anyone tel me how to get the image url.I want something similar to this
{"countries":[{"countryname":"India","flag":"http:\/\/wptrafficanalyzer.in\/p\/demo1\/india.png","language":"Hindi","capital":"New Delhi","currency":{"code":"INR","currencyname":"Rupee"}}
Below is my PHP code to get the data.
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!";
}**
Below is my upload PHP file
<?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>
Thanks in advance.