eblek
eblek

Reputation: 75

getting image with php and displaying in html

i know it is so simple problem but eventually it isn't working and i am a newbie. in the index.html, a swf sends an image and displayImage.php(below code) should display it on another page. why isn't it working??

<?php
if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
$no=0;
while (file_exists("images/$no.jpg"))
     $no++;
header('Content-Type: image/jpeg');
$image = $GLOBALS["HTTP_RAW_POST_DATA"];
file_put_contents("images/".$no.".jpg", $image);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml2/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-9" />
<title>Your Image</title>
<link href= "style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="logo"></div>
<div id="body"></div>
/////////display image//////////
<img src="images/<?$no.".jpg?>"> 

</body>
</html>

Upvotes: 0

Views: 247

Answers (5)

eblek
eblek

Reputation: 75

i deleted the line "header('Content-Type: image/jpeg');" and it works!

Upvotes: 0

Billy
Billy

Reputation: 23

Could be that you're using XHTML strict. IMG tags can't be unclosed like in normal HTML. You have to end it with a /> instead. (it's supposed to fail catastrophically when you make an error but it only does that when you send the right MIME type header to treat it as XML, so treated as text you may get unpredictable results.....). If it's printing the name of the file (like images/1.jpg) then you know that it's parsing the inline PHP correctly....

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 817030

You don't echo the filename (and you have a quoting error, but this could be a typo):

<img src="images/<?php echo $no ?>.jpg" />

Assuming that storing the file actually works.

Upvotes: 1

Nate Pinchot
Nate Pinchot

Reputation: 3318

This might be the issue. The line below the "display image" comment, should probably read:

<img src="images/<?= $no ?>.jpg"> 

Upvotes: 0

peelman
peelman

Reputation: 1349

<img src="images/<?=$no.".jpg"?>"> 

Upvotes: 0

Related Questions