tags:

views:

196

answers:

2

I have been trying to display image from database using php but i am only getting download prompt whenever i use or click the link. what i want to do is, i want to display it on the web browser. And want to use it in tags.

my code is:

require_once('dbconfig.php');
        $cont=mysql_connect($dbhost,$dbuser,$dbpass) or die("Error Connecting the Database");
        $seldatabase=mysql_select_db($dbselect,$cont);
        $insert = "SELECT `p_mime`, `p_name`, `p_size`, `p_data` FROM $dbtbl_reg_details WHERE `id` = $id";
        $query = mysql_query($insert);
        if ($query)
        {
            if (mysql_num_rows($query)==1)
            {
                $row = mysql_fetch_assoc($query);
                header("Content-Type: image/png");
                header("Content-Length: ". $row['p_size']);
                header("Content-Disposition: inline; filename=". $row['p_name']);
                echo $row[p_data];
            }
        else
        {
            echo "image with id = ".$id." does not exist";
        }
    }
    else
    {
        echo "query failed, image with id = ".$id." does not exist";
    }

And when i use inline for Content-Disposition: then my web page returns a script error on the browser.

So, what should i do to display images on web pages while retrieving it from mysql database

A: 

why u have echo hi; ?

its probably cause an error

and also add exit in the end (to sure you dont echo anything after)

echo $row[p_data];
exit;
Haim Evgi
hi i have removed that echo hi and made Content-Disposition: to inline.. now getting an error "The image “http://127.0.0.1/isc2011/photo.php?id=2” cannot be displayed, because it contains errors."I have uploaded the file properly, what could be the reason!?
Idlecool
A: 
if (mysql_num_rows($query)==1)
{
    while( @ob_end_clean() );

    header("Content-type:  image/png");
    header("Content-Length: ". $row['p_size']);
    header("Content-Disposition: attachment; filename=\"{$row['p_name']}\"");

    die($row[p_data]);
}
ovais.tariq
hi! this "' . $row['p_name'] . '"' is returning me a syntax error
Idlecool
i have edited the statement, check it out.
ovais.tariq
while( @ob_end_clean ); was the trick.. it helped :)
Idlecool