开发者

change header content-type in php

开发者 https://www.devze.com 2023-04-09 04:29 出处:网络
I am 开发者_JS百科using this code $query = mysql_query(\"SELECT Image, Col2, Col3 FROM table \");

I am 开发者_JS百科using this code

$query = mysql_query("SELECT Image, Col2, Col3 FROM table ");
$row = mysql_fetch_array($query);
$content = $row['Image'];
header('Content-type: image/jpg');
echo $content;  
echo $row['Col2'];
echo $row['Col3'];

The problem is that once i do 'Content-type: image/jpg' the other Col2 and Col3 do not show up. They are not images but just text rows. How can i display image and the text at same time?


The browser is expecting only image binary data when setting the header content type to image/jpg. Echoing out text with this content type will break the image binary and not display the text.

Without changing the header you could encode the image into an image tag:

echo '<img src="data:image/jpg;base64,' . base64_encode($content) . '" />';
echo $row['Col2'];
echo $row['Col3'];


You can't per se...

You are sending a content type of image data which means all the data you send is interpreted as bytes of data forming an image.

The only way you could see your data is use the header() function to sent abitrary data that is not useful for the browser. Then using firebug or wathever browser console you use, lookup the NETWORK tab and open up your request for the image and check the headers tab, you will see your data there but you can't show that data to the user.

header('mydata2: '.$row['Col2']);
header('mydata3: '.$row['Col3']);
header('Content-type: image/jpg');
echo $content;

The other weird way would be to print your text on the image using GD, but thats probably not waht you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号