Jump to content

blob stpred image showing as hex.. nowhere left to move header info??


hugeness

Recommended Posts

Hi

 

I am using the following code...

 

$conn = mysql_connect('localhost','root','') or trigger_error("SQL", E_USER_ERROR); 
$db = mysql_select_db('minisite',$conn) or trigger_error("SQL", E_USER_ERROR); 


$id = 1;

$type = $row['type'];
header('Content-type: $type');

$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
//line 21 ->
while($row = mysql_fetch_array($query)){
$content = $row['content'];

echo $content;
}

 

 

adapted, just to try and experiment and get started with showing an image stored as a blob in my database..

 

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\ggg\My Documents\php\xampp\htdocs\minisite\graphic_test1.php:1) in C:\Documents and Settings\ggg\My Documents\php\xampp\htdocs\minisite\graphic_test1.php on line 17

GIF89a–÷ÿ‰Œ”©­¹¡¥´’•¢jkrøùú+()ƒŒ•™¤ÁÅÓ¡«ÑÕᚊ™424¥©µïðóðòõ¹½Ê

 

etc etc

 

if anyone can move me on a bit, would be great!

thank you.

Link to comment
Share on other sites

Http responses have two parts: a header and a body.  You *must* finish sending all of the header information before the body.

 

Various PHP functions will send header information, including but not limited to: session_start(), header()

 

If you use an echo, as in echo 'hi there' and then do something that modifies header information, you will get this error.

 

I promise you, somewhere you are sending part of the body before you finish with the header info, or you would not get this message.

 

A common source of this problem is trailing white space in a file:

 

<?php
// image.php
include( 'db.php' );
header( 'Content-Type: image/jpg' );
echo $image;
?>

 

<?php
// db.php
mysql_connect( /* stuff */ );
// notice there is a trailing newline at the end of this file!!!
?>

Link to comment
Share on other sites

  • 4 weeks later...

Thanks for that... got the image to display after upload.. this is a form review page, where the user checks the details that they have put in, including the graphic and text.. at the moment i can only get either the text, OR the graphic to display, but not both.. again i assume this is a header issue.. i have looked around a lot, but so far can only glean that there is a way to get both to work using an include file, but not seen enough detail to work out what is going on..

here is my somewhat messy but basically functional code..

<?php 

$_SESSION['othf']= $_POST['othf'];
$_SESSION['newRight']= $_POST['newRight']; 
$_SESSION['un']= $_POST['un'];............................

passing the variables from the previous page.. all works ok

..............$off_yn = $_POST['off_yn'];
$off_more = $_POST['off_more'];
$ts5 = $_POST['ts5'];


//upload file/logo
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

$conn = mysql_connect('localhost','root','') or trigger_error("SQL", E_USER_ERROR); 
$db = mysql_select_db('minisite',$conn) or trigger_error("SQL", E_USER_ERROR); 


$query = "INSERT INTO upload (name, size, type, content,pw,un ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content','$pw','$un')";

mysql_query($query) or die('Error, query failed');
//include '../library/closedb.php';

}

$row = mysql_fetch_array(mysql_query("SELECT content FROM upload WHERE id = LAST_INSERT_ID() ")); 
header("Content-type: image/jpg"); 
print $row['content'];  


<!-- NOTHING SHOWS BELOW THIS POINT -->

$tc4 = $tc1.'<br/> '.$tc2.'<br/> '.' '.$tc3;
?> 
have tried this next part echoed out as a php string, no success..

<html><head><title>Upload Logo</title>
<style type="text/css">

a {
color: #e16500;
}
STYLE SHEET BITS HERE

</style></head>

<body><div id="main"><p>The following information was successfully added to our database ... please take a few moments to review and update as necessary:</p>
<h3>Registration information created: $cre </h3>
<p>File $fileName uploaded</p>
<p>Your Username is $un and your password is $pw.</p>

<p>You are: $fn $ln, $ttl</p>
LOTS OF MIXED HTML AND VARIABLES 

...... ype="hidden" name="ts7" value=" $ts7">
<input type="hidden" name="ts5" value=" $ts5">
<input type="submit" input id="update" value="Click here to update your details" >
</form>   </body>
</html>

 

anyone know of any good examples for this setup?

also.. have seen an example which again i dont quite understand, but could work for me.. instead uploading the graphic converted to base64.. does anyone know if this method works around the conflict displaying a blob and text appears to have?

heres an example of converting to base64.. http://www.tutcity.com/view/image-databasing.9514.html

 

thank you!

Link to comment
Share on other sites

Every image on a web page must use an <img ...> tag - http://w3schools.com/html/html_images.asp

 

The URL that you put into the src="..." attribute must be a URL that outputs an image or for the case where you are dynamically outputting an image using php, the URL is that of the .php page that outputs the correct content-type header followed by the image data.

Link to comment
Share on other sites

Every image on a web page must use an <img ...> tag - http://w3schools.com/html/html_images.asp

 

The URL that you put into the src="..." attribute must be a URL that outputs an image or for the case where you are dynamically outputting an image using php, the URL is that of the .php page that outputs the correct content-type header followed by the image data.

 

yep, only i'm not outputting the image with a reference, i am downloading it from the database.. the graphic is in the database as hex code.. not referenced as a URL.

thanks though, i always love that site!

Link to comment
Share on other sites

// BLOCK A
$row = mysql_fetch_array(mysql_query("SELECT content FROM upload WHERE id = LAST_INSERT_ID() ")); 
header("Content-type: image/jpg"); 
print $row['content'];  


<!-- NOTHING SHOWS BELOW THIS POINT -->
// BLOCK B
$tc4 = $tc1.'<br/> '.$tc2.'<br/> '.' '.$tc3;
?> 
have tried this next part echoed out as a php string, no success..
<!-- BLOCK C -->
<html><head><title>Upload Logo</title>

 

Le sigh.  You send one of two things to the browser:

Y) plain text, which includes html, xhtml, csv, text, xml, etc.

Z) binary data, pdfs, excel files, images

 

You do not mix the two together.

 

I've commented your code as blocks A, B, and C.

 

In Block A you are selecting binary data from the database.  Then you are telling the browser what comes next is an image/jpg with your call to header().  Then you are dumping the binary data.  By itself that is fine.

 

But then in Block B you start outputting plain text.  You are trying to send Y (plain text output) and Z (binary output) together at once.  This you can not do.

 

And then in Block C you are starting an actual html document with an html-tag.  When outputting an html-tag it should be the first thing (except for maybe a doctype).  But you've already sent binary data and then html tags br.  So you're all over the place.

 

What you need to do is remove Block A and Block B.

 

Then in Block C where you have your HTML document, you need to add an img-tag with a src-attribute set:

<img src="/showimage.php?image_id=<?php echo $last_insert_id; ?>" />

 

Then you need to create a file named showimage.php and you need to move your BLock A into that file.  You will need to make some adjustments to make it work correctly.

 

Block B is probably unnecessary altogether, but that depends on what's inside of: $tc1, $tc2, and $tc3.

Link to comment
Share on other sites

ok.. so this is what i did, i built the include page... cutting the code from the main page...

 

showimage.php

 

$conn = mysql_connect('localhost','root','') or trigger_error("SQL", E_USER_ERROR); 
$db = mysql_select_db('minisite',$conn) or trigger_error("SQL", E_USER_ERROR); 


$row = mysql_fetch_array(mysql_query("SELECT * FROM upload ORDER BY id DESC LIMIT 1;")); 
header("Content-type: image/jpg"); 
print $row['content']; 

 

and inserted the code suggested with a little edit.. as suggested.. in the main page..

 

<p><img src="showimage.php?image_id=<?php echo $row['content']; ?>" /></p>

 

worked like a dream.. thanks a million! le goodstuff.

 

Link to comment
Share on other sites

You're going to find that your code won't work as you add more images or multiple people are uploading images at the same time.  The ?image_id=$LASTINSERTID should have the database primary key of the uploaded image.

 

Then in your showimage.php file you should be selecting from the database where id=$_GET['image_id'].

Link to comment
Share on other sites

Ahh, thanks for that.. so just to clear it for me, LAST_INSERT_ID() will select the last primary id for the existing session, so even if others are updating the database at the same time, that user will be editing the same record set?

 

will have another go modifying the code...

Link to comment
Share on other sites

OK.. so now i have grabbed the row ID using

$rowID=mysql_insert_id(); so i have the auto increment id for the row, this means no mixup when adding graphics, and i'm now using the same table, just simpler for me, shouldnt be too much overhead.. only going to have a few hundred users total..

 

passing all the form data on to the page where the graphic gets uploaded in a session..

so, i should now be able to upload the graphic to the same row, using the same table... as an update, and i can, this works..

however, when i try to select the row using "SELECT * FROM table where id='$rowID'; in the include page showimage.php

the query doesnt work.. if i put the row number in, 24, then the query works. and if i lift the query out of the include page (leaving the header info) and put it on the main page i get the hex code... so it seems the include page cannot get a handle on the variable $rowID.

how can i resolve this?

Link to comment
Share on other sites

I need to see the full source for the two files in question: showimage.php and whatever PHP page is trying to create the HTML img-tag.

 

Remember to remove any database connection or other sensitive information out of them.

 

It's been a while since I used mysql_insert_id(), but from memory it returns the last value generated by an auto-incrementing column for your current database connection / handle.  Therefore yes, it is independent of other users.  However if you ran several queries in a row where each query caused an auto-increment to occur, this function would return only the last auto-incremented value.

 

Read the PHP documentation on the function if you're not sure and check the user comments as well.  It's almost always useful!

Link to comment
Share on other sites

ahhh... i did the $last2ID = mysql_insert_id();

echo $last2ID; on the previous page, where the bulk of the form was inserted into the database, then passed the id as a variable in the session, fixing it for that user..

 

the main page where the graphic is uploaded as an update, then displayed.....

 

<?php 
$_SESSION['last2ID']= $_POST['last2ID'];
$_SESSION['othf']= $_POST['othf'];

****lots of variables passed in the session....****

$_SESSION['ts15']= $_POST['ts15'];
$_SESSION['ts16']= $_POST['ts16'];
$_SESSION['ts17']= $_POST['ts17'];

$ts7=serialize($_POST['ts7']); //takes the data from a post operation...
$ts8 = $_POST['ts8'];

$last2ID = $_POST['last2ID'];
$ts9 = $_POST['ts9'];
$ts10 = $_POST['ts10'];
$ts11 = $_POST['ts11'];
$ts12 = $_POST['ts12'];
$ts13 = $_POST['ts13'];

*****etc etc variables carried from last page...*****

$off_yn = $_POST['off_yn'];
$off_more = $_POST['off_more'];
$ts5 = $_POST['ts5'];

*****the script to update the row where all the stuff above has just been added to the database... this is updating in the right place..*****

//upload file/logo
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

// connect to db
include '..database include.php';


$query = ("update $table1 SET name = '$fileName', size = '$fileSize', type = '$fileType', content ='$content' 
where id = $last2ID");

mysql_query($query) or die('Error, query failed');
}



$tc4 = $tc1.'<br/> '.$tc2.'<br/> '.' '.$tc3;

?> 
<html><head><title>Upload Logo</title>
<style type="text/css">

*****style sheet************</style></head>

<body><div id="main">
<p>The following information was successfully added to our database ... please take a few moments to review and update as necessary:</p>
<h3>Registration information created:<?php echo $cre; ?> <?php echo $last2ID; ?></h3>

********echo $last2ID correctly pulling out the id number of the row for this dataset***********

<p>File<?php echo $fileName ; ?> uploaded</p>
<p>Your Username is <?php echo $un; ?> and your password is <?php echo $pw; ?>.</p>

<p>You are: <?php echo $fn; ?><?php echo  $ln;?>, <?php echo $ttl; ?></p>

<p>Username: <?php echo $un; ?>, password: <?php echo $pw; ?></p>
<p>Phone number: <?php echo $tel; ?></p>
<p>Your email: <?php echo $conteml; ?></p>

***************details***********

<h3>Geographics</h3>
<p>Do you focus Nationwide?:<?php echo $nw; ?></p>

*************where the logo is displayed as they uploaded it to check***************



<p>Your logo:</p>

*************pulling the graphic from the database**********************

<p><img src="showimage.php?image_id=<?php echo $row['content']; ?>" /></p>


bunch of hidden variables to pass to next page 

<input type="hidden" name="tst1" value=" $tst1">
<input type="hidden" name="tst2" value=" $tst2">
<input type="hidden" name="tst3" value=" $tst3">
<input type="hidden" name="tst4" value=" $tst4">
<input type="hidden" name="tst5" value=" $tst5">
<input type="hidden" name="tst6" value=" $tst6">
<input type="hidden" name="nw" value=" $nw">
<input type="hidden" name="pr" value=" $pr">
<input type="hidden" name="ps1" value=" $ps1">
<input type="hidden" name="ps6" value=" $ps6">
<input type="hidden" name="ps16" value=" $ps16">
<input type="hidden" name="ps26" value=" $ps26">
<input type="hidden" name="ps46" value=" $ps46">
<input type="hidden" name="ps100" value=" $ps100">
<input type="hidden" name="cent1" value=" $cent1">
<input type="hidden" name="cent6" value=" $cent6">
<input type="hidden" name="cent16" value=" $cent16">
<input type="hidden" name="cent26" value=" $cent26">
<input type="hidden" name="cent46" value=" $cent46">
<input type="hidden" name="cent100" value=" $cent100">
</form>   </body>
</html>

 

 

the showimage.php page...

 

<?php
// connect to db
include '..database include.php'; 
//$row = mysql_fetch_array(mysql_query("SELECT * FROM duplicate where id='24';")); when the row is set to id 24, the script works..
$row = mysql_fetch_array(mysql_query("SELECT * FROM duplicate where id='$last2ID';"));
//this doesnt appear to be finding the variable 
header("Content-type: image/jpg"); 

?>

Link to comment
Share on other sites

See my comments in your code, which start with: ###

 

Your main page:

<?php 
$_SESSION['last2ID']= $_POST['last2ID'];
$_SESSION['othf']= $_POST['othf'];

****lots of variables passed in the session....****

$_SESSION['ts15']= $_POST['ts15'];
$_SESSION['ts16']= $_POST['ts16'];
$_SESSION['ts17']= $_POST['ts17'];

$ts7=serialize($_POST['ts7']); //takes the data from a post operation...
$ts8 = $_POST['ts8'];

$last2ID = $_POST['last2ID'];
$ts9 = $_POST['ts9'];
$ts10 = $_POST['ts10'];
$ts11 = $_POST['ts11'];
$ts12 = $_POST['ts12'];
$ts13 = $_POST['ts13'];

*****etc etc variables carried from last page...*****

$off_yn = $_POST['off_yn'];
$off_more = $_POST['off_more'];
$ts5 = $_POST['ts5'];

*****the script to update the row where all the stuff above has just been added to the database... this is updating in the right place..*****

//upload file/logo
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

// connect to db
include '..database include.php';

### ASSUMING THAT $last2ID IS VALID AND REFERENCES THE ROW IN QUESTION, THEN
### THIS PART IS FINE.  NOW SEE MY NOTE BELOW.

$query = ("update $table1 SET name = '$fileName', size = '$fileSize', type = '$fileType', content ='$content' 
where id = $last2ID");

mysql_query($query) or die('Error, query failed');
}



$tc4 = $tc1.'<br/> '.$tc2.'<br/> '.' '.$tc3;

?> 
<html><head><title>Upload Logo</title>
<style type="text/css">

*****style sheet************</style></head>

<body><div id="main">
<p>The following information was successfully added to our database ... please take a few moments to review and update as necessary:</p>
<h3>Registration information created:<?php echo $cre; ?> <?php echo $last2ID; ?></h3>

********echo $last2ID correctly pulling out the id number of the row for this dataset***********

<p>File<?php echo $fileName ; ?> uploaded</p>
<p>Your Username is <?php echo $un; ?> and your password is <?php echo $pw; ?>.</p>

<p>You are: <?php echo $fn; ?><?php echo  $ln;?>, <?php echo $ttl; ?></p>

<p>Username: <?php echo $un; ?>, password: <?php echo $pw; ?></p>
<p>Phone number: <?php echo $tel; ?></p>
<p>Your email: <?php echo $conteml; ?></p>

***************details***********

<h3>Geographics</h3>
<p>Do you focus Nationwide?:<?php echo $nw; ?></p>

*************where the logo is displayed as they uploaded it to check***************



<p>Your logo:</p>

*************pulling the graphic from the database**********************
<?php
### WE PASS THE ROW ID TO THE SHOWIMAGE.PHP SCRIPT...WE DO NOT PASS $row['content']
### THIS SHOULD BE THE ROW WE UPDATED EARLIER IN THIS SCRIPT.
?>
<p><img src="showimage.php?image_id=<?php echo $last2ID; ?>" /></p>


bunch of hidden variables to pass to next page 

<input type="hidden" name="tst1" value=" $tst1">
<input type="hidden" name="tst2" value=" $tst2">
<input type="hidden" name="tst3" value=" $tst3">
<input type="hidden" name="tst4" value=" $tst4">
<input type="hidden" name="tst5" value=" $tst5">
<input type="hidden" name="tst6" value=" $tst6">
<input type="hidden" name="nw" value=" $nw">
<input type="hidden" name="pr" value=" $pr">
<input type="hidden" name="ps1" value=" $ps1">
<input type="hidden" name="ps6" value=" $ps6">
<input type="hidden" name="ps16" value=" $ps16">
<input type="hidden" name="ps26" value=" $ps26">
<input type="hidden" name="ps46" value=" $ps46">
<input type="hidden" name="ps100" value=" $ps100">
<input type="hidden" name="cent1" value=" $cent1">
<input type="hidden" name="cent6" value=" $cent6">
<input type="hidden" name="cent16" value=" $cent16">
<input type="hidden" name="cent26" value=" $cent26">
<input type="hidden" name="cent46" value=" $cent46">
<input type="hidden" name="cent100" value=" $cent100">
</form>   </body>
</html>

 

Your showimage.php

<?php
### WE NEED TO GET THE IMAGE ID FROM THE URL
$last2ID = array_key_exists( 'image_id', $_GET ) ? $_GET['image_id'] : false;

### WE'RE OUTPUTTING A JPG
header("Content-type: image/jpg"); 

### IF WE CAN'T PULL THE IMAGE FROM THE DATABASE, WE NEED TO OUTPUT SOME SORT OF IMAGE
$bad_image = '/path/to/image/if/a/bad/image/is/specified.jpg';

### WILL BE FALSE IF WE COULD NOT GET THE IMAGE FROM DATABASE
$image_contents = false;

if( is_numeric( $last2ID ) ) { ### ONLY IF THE ID IS NUMERIC DO WE PROCEED
    include '..database include.php'; 
    ### RUN OUR QUERY
    $q = mysql_query( "SELECT `content` FROM `duplicate` WHERE `id`={$last2ID}" );
    if( $q ) { ### TEST FOR SUCCESS
        ### FETCH THE ROW (I LIKE IT AS AN OBJECT)
        $row = mysql_fetch_object( $q );
        if( $row ) { ### WE GOT THE ROW
            ### WE GOT THE CONTENTS
            $image_contents = $row->content;
        }
    }
}

### EITHER $image_contents IS THE IMAGE CONTENTS FROM THE DATABASE OR FALSE
### IF IT'S FALSE, WE'LL USE THE CONTENTS OF OUR PLACEHOLDER IMAGE AND SEND
### THAT INSTEAD.
if( $image_contents === false || ! strlen( $image_contents ) ) {
    $image_contents = file_get_contents( $bad_image );
} 
echo $image_contents;
?>

Link to comment
Share on other sites

Good.

 

Now that it's working here is another thing to point out:

$content = addslashes($content); ### MOVE THIS LINE
fclose($fp);

if(!get_magic_quotes_gpc())
{
   ### MOVE IT INSIDE THIS BLOCK
    $fileName = addslashes($fileName);
}

 

I hope you understand the changes I'm having you make too.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.