cannot display image in PHP
here's my code for image display -
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$databa开发者_如何学Pythonse = "xxxxxxxx";
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$query = mysql_query("SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1");
$row = mysql_fetch_array($query);
$content = $row['Image'];
header('Content-type: image/jpg');
echo $content;
This is the error i'm getting
The image “http://www....” cannot be displayed because it contains errors.
what is wrong? The datatype of field in mysql is mediumblob
OK, first test, to see what is happening:
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";
if( !mysql_connect($host, $username, $password) )
die( 'Unable to connect to Server: '.mysql_error() );
if( !mysql_select_db($database) )
die( 'Can not select the Database: '.mysql_error() );
$query = mysql_query( "SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1" );
if( !$query )
die( 'Query Failed: '.mysql_error() );
if( mysql_num_rows( $query )==0 )
die( 'Query Returned No Records' );
$row = mysql_fetch_array($query);
echo '<pre>';
var_dump( $row );
echo '</pre>';
That should either show you the results from the database, or an error message. If you see an error message, correct whatever is causing it...
After the above just returns the Database row contents:
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";
if( !mysql_connect($host, $username, $password) )
die( 'Unable to connect to Server: '.mysql_error() );
if( !mysql_select_db($database) )
die( 'Can not select the Database: '.mysql_error() );
$query = mysql_query( "SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1" );
if( !$query )
die( 'Query Failed: '.mysql_error() );
if( mysql_num_rows( $query )==0 )
die( 'Query Returned No Records' );
$row = mysql_fetch_array($query);
header( 'Content-type: '.$row['mimetype'] );
echo $row['Image'];
(assuming that the mimetype
field is something like "image/jpg")
I'd suggest adding a content-length header before your output:
header("Content-length: " . strlen($content))
I'd suggest to change the header to make it dynamic depend on the image mime-type:
header('Content-type: '.$row['mimetype']);
精彩评论