Header Already sent Problem For Images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aftabnaveed
Forum Newbie
Posts: 9
Joined: Sat Sep 30, 2006 1:05 am

Header Already sent Problem For Images

Post by aftabnaveed »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, Before listing my problem I would excuse for my English... because i am not a native english speaker
I am trying to read an image from the client side and read the contents of the images through

Code: Select all

<?php
$picture =  $_FILES["picture"]["tmp_name"];
$fp = fopen($picture,"r");	
$content = fread( $fp,filesize($picture) );
?>

Now I am trying to display Image inside a table like

Code: Select all

<table>
    <tr>
        <td><?php
             header("Content-type:image/jpeg");
             echo($content);
            ?>
         </td>
     </tr>
  </table>
When I try put the header("Content-type:image/jpeg"); line then it says that header already sent error message.

When I try to avoid this i use ob_start() and ob_flush() to display the image, but it only prints the URL from the address bar of the browser. But if I send header("Location:someotherpage.php"); It works well
So Is there any solution for this problem to display the Images header inside the html page


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Header Already sent Problem For Images

Post by volka »

aftabnaveed wrote:When I try put the header("Content-type:image/jpeg"); line then it says that header already sent error message.
And it tells you the file:line where the output occurred. Please mark that line in your code.
Now I am trying to display Image inside a table like
<table>
<tr>
<td><?php
header("Content-type:image/jpeg");
echo($content);
?>
</td>
</tr>
</table>
Images in html are referenced as <img src="....">. You must not print the binary image data within the html document. (there's a small exception to this rule, but it's negligible)
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Printing the URL when you use Content-type:image/jpeg means that the $content does not contain valid jpeg image. if you view the source of the page on which you get this error you will see what is the problem, but i guess it is the HTML code after the echo.

It breaks the JPEG and makes it invalid for displayng.
aftabnaveed
Forum Newbie
Posts: 9
Joined: Sat Sep 30, 2006 1:05 am

Post by aftabnaveed »

Thanks for your reply....

As for as header information is concernd i am trying to display the image on line 169 of my html document
and one other thing i have experiments my code in a seprate file before i have been sending any header in any form and it worked for me well,,, So the Question arises that How can I send multiple headers in php. I have user ob_start() and ob_flush() functions but it didn't worked for me at all The Code which successfully executed is

Code: Select all

$picture =  $_FILES["picture"]["tmp_name"];
		$fp = fopen("$picture","r");
		$content = fread($fp,filesize($picture) );
		
		header("Content-type: image/jpeg");
		echo( $content );
but the above code is for testing purpose in a seprate file thats why i have tried to use it also in html

here is the header error:
Warning: Cannot modify header information - headers already sent by (output started at F:\wamp\www\realestate\includes\application_top.php:18) in F:\wamp\www\realestate\includes\pages\modules\members\forsale\view_forsale.php on line 169
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

aftabnaveed wrote:So the Question arises that How can I send multiple headers in php.
You can't if what you are trying to do is first tell the browser that the page is text/HTML and then you're trying to tell it that it's an image instead. That's why if you want an image to display in a webpage you need to use the IMG tag. You can do the following:

Code: Select all

<img src="myphpimagescript.php" alt="" />
i.e. you can call a PHP script in the src attribute if you need to process the image with PHP before displaying it (and in that file - myphpimagescript.php - you would set the headers to be image headers).

Mac
aftabnaveed
Forum Newbie
Posts: 9
Joined: Sat Sep 30, 2006 1:05 am

Post by aftabnaveed »

Can you explain it a little bit more I have tried this
"upload.php"

Code: Select all

<?php
  session_start();
  if( isset( $_POST["Submit"] ) )
  {
  	$picture = $_FILES["picture"]["tmp_name"];
	$_SESSION["picture"] = $picture;
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<p>
<input name="picture" type="file" id="picture" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
<img src="image.php" />
</body>
</html>
AND image.php

Code: Select all

session_start();
        $picture =  $_SESSION["picture"];
		$fp = fopen("$picture","r");
		$content = fread($fp,filesize($picture) );
		
		header("Content-type: image/jpeg");
		echo( $content );

but it didn't worked for me... Can you provide some sample code from which I should sovle my problem
Thanks in Advance
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$picture = $_FILES["picture"]["tmp_name"];
php removes all temporary files when the script (in this case upload.php) is done.
Use move_uploaded_file to store the file in a "save" place.
aftabnaveed
Forum Newbie
Posts: 9
Joined: Sat Sep 30, 2006 1:05 am

Post by aftabnaveed »

I don't wana save the file on my server, I want to show the user the preview of the I mages then if he/she wants to save the i mage then it will be saved on the server.
So Please help me on retrieving the temperory file will be thankful to you guys
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

you must save it on your server first, then display it.

For this purpose you can make temp directory which saves the files and if the user decides to save it when he see it you will move it from temp directory to the proper location.

You will ask now what will be happen with the files in the temp folder if user decides to not save the image. Yea their number will grow fast, but you can run a cron which checks the file timestamps and if older than 1 day -> unlink()

Don't forget to check the file before accept it in the temp folder, chekcing the extension or mime type is not enough. You can use the GD lib to check the images with imagecreatefromjpeg if this returns false this means the file is not jpeg even its extension is jpg ;)
aftabnaveed
Forum Newbie
Posts: 9
Joined: Sat Sep 30, 2006 1:05 am

Post by aftabnaveed »

Thanks for your suggession I totaly understand what you are saying,, but what if the Server I am hosting my site is Windows Server
and, Cron also have a version for windows what if its not installed there....
Please guide me if i am saying some thing wrong
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

On windows servers the crons are called sheduled tasks. If you don't have access to set up it, you can put the checking code in the same file which uploads the image, so when someone tries to upload image the script will do the cleaning procedure.
Post Reply