here is the code
<?php
session_start();
if(!isset($_SESSION['user_name']))
{
header('Location: login.php');
}
$conn = mysql_connect("localhost", "root", "") or die("Can no connect to Database Server");
?>
<html>
<head>
</head>
<body>
<center>
<div id="ser">
<form action="" method="post">
<label for="file">Card No:</label>
<input type="text" name="card_no" id="card_no" class="fil" onKeyUp="CardNoLength()" onKeyDown="CardNoLength()" onKeyPress="CardNoLength()"/>
<input type="submit" name="search" value="Search" class="btn" onClick="return CardNoLengthMIN()"/>
</form>
</div>
</center>
<br/><hr style="border: 1px solid #606060 ;" />
<center><a href="index.php">Home</a></center>
<br/>
<center>
<?php
if(isset($_POST['card_no']))
{
if($conn)
{
if(mysql_select_db("img_mgmt", $conn))
{
$sql = "select * from temp_images where card_no='".trim($_POST['card_no'])."'";
$result开发者_StackOverflow = mysql_query($sql);
$image = mysql_fetch_array($result);
if(isset($image['card_no']))
{
//echo "<img src=\"".$image['file_path']."\" alt=\"".$image['card_no']."\" width=\"250\" height=\"280\"/>";
header("Content-type: image/jpeg");
echo $image['img_content'];
}
else
{
echo "<p style=\"color:red;\">Sorry, Your search came with no results ! <br/> Try with different card number";
}
}
else
{
echo "Database selection error: ".mysql_error();
}
}
else
{
echo "Could not connect: ".mysql_error();
}
}
?>
</center>
</body>
</html>
But it after executing the script it shows:
Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\img\search.php:61) in C:\xampp\htdocs\img\search.php on line 77
If you have save the content of the image in the database you can use a data:
uri do show this content in a <img />
tag. This allows you to embed content of different mime type in another content, see data URI scheme.
But you shouldn't really save any files (regardless of image or not) in the database. Files belongs to the filesystem, as the name already suggest. You get a huge overhead in saving files in the database. Specially for images you need (maybe) a php script which loads the image, eg. if you use something like <img src="showimage.php?id=5" alt="..." />
. For each image you need to call an additional php script, and you gain nothing. Everyone will tell you its better to save the files in the filesystem and load it via the filesystem as normal. So you use tags like <img src="images/foobar/xyz.png" alt="..." />
instead. Even the "I don't want any 'broken links' to images in my database" argument doesn't count as you simply use the ID inside the path and use file_exists()
to check if an image link is valid or not.
$path = 'images/useravatars/'.$row['ID'].'.png'; // as an example
if (file_exist($path)) {
echo '<img src="'.$path.'" alt="username" />';
} else {
echo '<img src="images/noimage.png" alt="No Image found" />';
}
The reason you are getting that error is because you are trying to send the header indicating that the file is an image, but you can't send headers after you output something (in this case, the HTML outside the PHP tags is the offending output).
However, that is not your main problem. The real problem you are having is that you can not simply output an image into a HTML file like that to display it (Truthfully, you can encode them into image tags, but that's not recommended). What you are trying to do right now, is that you send some HTML, then try to tell the browser that the page is actually an image and then send the image content. That just doesn't work. You have to do it just like with regular HTML files. The images need to be separate files that the <img>
tag links to.
In other words, you need the script that outputs the page and another script that the <img>
tags link to, which fetches a single image from database and and diplays it. So, instead of trying to output the image on that page, you should output a tag with url like show_image.php?id=123
, which links to the script that outputs the image with the appropriate ID from the database.
you cannot mix-match html and image data, you'd have to create two scripts. one that generates the html with an <img />
tag and one that will create the actual image.
the error you are getting is because you cannot set headers after your script has output something, you can use ob_start
to work around this
Don't you know HTML yet? How this code supposed to work?
Why not to store images in the filesystem, if you don't even know how to show it from the database?
Use ob_start() on the top of page. This cleans the buffer
eg.
< ?php
ob_start();
session_start();
You are getting the header erorr, try putting below on top of your file:
ob_start();
If still the same issue, you can redirect to a new page using javascript but it is not the best method:
if(!isset($_SESSION['user_name']))
{
?>
<script>
document.location.href = 'login.php';
</script>
<?php
}
Your script
C:\xampp\htdocs\img\search.php
is generating output on line 61. HTTP headers must go first, they cannot be sent in the middle of a document.Setting an HTTP header does not stop the current script. You must explicitly call exit().
All the ob_start
answers are wrong. Your code will never work like this. You can put links to images in an HTML page, but you can't put image data directly into it. You have to make an HTML page, and another script which will display the image, and you will call it from the first page with a <image src="imagescript.php">
tag.
Move your code to another script and then just use something like:
<img src="myimg_genarator_script.php" />
And please think about using a framework, even if it's a small project there are plenty of frameworks that will be helpful for you and won't cause much overhead (try codeigniter)
精彩评论