2

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.

Grant
  • 2,413
  • 2
  • 30
  • 41
user2211549
  • 37
  • 1
  • 5

1 Answers1

1

What you are trying to do is not going to work. In your code you store the image data in the database table. Your method to return the JSON encoded image information and location does contain the image data from the table, you can't send the image data in JSON for displaying an image. You need to change your method that returns the JSON data so that it does not read the image data from the table. What you need to do is to insert an entry that is the URL you are asking for in you question:

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

Then you need another PHP script that returns the image from the database and is running when the above URL is invoked. The PHP script would query the database and return a HTTP response with a proper Content-Type header for the image type. For an example look at the answer for How to retrieve images from MySQL database and display in an html tag

Community
  • 1
  • 1
Bernhard
  • 8,583
  • 4
  • 41
  • 42