Jump to content

[SOLVED] header work around problem (yes I read ur header posting)


willaguila

Recommended Posts

The code below is part of a main page script and by all means there is much more to it but there is no need to put it

all here as all I need is a work around idea. The script below basically calls a few Classes that access an image in the

database and displays the image on a page. I would like to put the script below as part of the class and call the object

which echos the image onto the page, but as you see my echo statement is in the for loop and the header info for,

datatype = image type and name = whatever file name, remains on the main script page and that is a no no which

produces our headers already sent error. If you have any ideas as how to add this to a class and send the headers

before the processing please enlighten me? Note: the images are pulled out of the database as a stream of 64k

chunks, hence the for loop below! :confused:

 

 

 

    header ( "Content-Type: $FileObj->datatype" );

    header ( "Content-Name: $FileObj->name" );

 

 

 

for ($Z = 0 ; $Z < count($nodelist) ; $Z++) {

    $sql = "SELECT * FROM filedata WHERE id = " . $nodelist["$Z"];

 

    if (!$resultx = $database->query($sql)) {

      die("Failure to retreive file node data");

    }

 

    $DataObj = $database->fetch_object($resultx);

    //$DataObj->filedata;

echo $DataObj->filedata; //cannot be placed in object because of header

 

  }

Link to comment
Share on other sites

This is not really a header issue. You CANNOT output an image on a web page by directly outputting the image data on the page. You must use a HTML <img tag for each image. The browser then fetches the image from the URL that is in the src='...' attribute.

 

Ref: http://w3schools.com/html/html_images.asp

 

To use an image that is produced by php, the URL that you put into the <img tag would be to the .php script that outputs the header followed by the image data. You would typically include an ?image=some_identifier GET parameter on the end of the URL that specifies which image to produce and output.

 

 

Link to comment
Share on other sites

Sorry to differ but I already have it working with out the class and it outputs the image right on a page with the header and the echo below:

 

    header ( "Content-Type: $FileObj->datatype" );

    header ( "Content-Name: $FileObj->name" );

 

  echo $DataObj->filedata

 

 

I can send you a link to show you it works well if you like?

My problem is not getting the stream to work but how to put the for loop into a class and sending the

header info before the echo?

 

 

 

Link to comment
Share on other sites

You can output ONE image at a time as the only thing on a page and it will work because the browser can correctly decode what you sent it. This is the same as if you browsed directly to the URL in an <img tag.

 

You CANNOT output more than one image at a time and you cannot output the image data directly on a HTML web page.

Link to comment
Share on other sites

Well, I think your missing the idea here! The script I created will display the image directly to the browser and will create an image on the fly as the stream is assembled by the for loop you see above and yes it is one image at the time and the browser creates its own img tag! But you can also output more than one image at the time in difference to your opinion if you do this for example:

 

 

If I use a php page with a couple of img tags that calls my script above then I can display as many images

as I want to the page on the fly, as so:

 

echo "<img src=\"http://download_image.php?id=1\"  style=\"cursor: auto;\" />";

echo "<img src=\"http://download_image.php?id=2\"  style=\"cursor: auto;\" />";

echo "<img src=\"http://download_image.php?id=3\"  style=\"cursor: auto;\" />";

 

Anyway this works no problem for me and I can resize the image, place it a css or table model etc.., I just thought someone had some idea on how to put that for loop into a class and I could send the headers before the echo?

 

 

Link to comment
Share on other sites

You ARE doing exactly what I posted was needed to make images work on a web page. Are you high on something or are you just trying to spam this forum?

 

What you just posted has absolutely nothing to do with the code in the first post where you were trying to output one set of headers followed by multiple sets of image data.

Link to comment
Share on other sites

I have a suspicion that the loop isn't individual images, but several chunks of raw binary data that comprise a single image:

Note: the images are pulled out of the database as a stream of 64k chunks, hence the for loop below!

 

Why not put the headers in the constructor for your class rather than the main calling script?

Link to comment
Share on other sites

Moderator:

Listen, I do not need to be insulted, I am not high!!!!

I'm trying to make my code better and in the process maybe teach

someone else something they do not already know! Or is that what the call spam these days  ;)....

 

  The script I posted at the start of the thread is the script that calls the image if you follow!

  That script sends out header information telling the browser that it is an image!

 

  You stated that I cannot send image data directly to an html page and I quote:

      "You CANNOT output an image on a web page by directly outputting the image data on the page"

 

  Well, that is exactly what the script above is doing and it works!

 

  You then corrected yourself that well you can only send 1 image to a page and I quote:

  "You can output ONE image at a time as the only thing on a page and it will work because

    the browser can correctly decode what you sent it."

 

  I replied, that I can call the original script below with an img tag, and I can place as

many images as I want in an HTML page even though the script below is sending the

header info it works no problem!

 

Anyway Mr. Moderator, my original question had nothing to do with the image problem you brought up.

I can download that image from the database, place it on any html page and manipulate it any way

I want! The original question was:

 

If you have any ideas as how to add this to a class and send the headers

before the processing please enlighten me? Meaning this:

 

header ( "Content-Type: $FileObj->datatype" );

    header ( "Content-Name: $FileObj->name" );

 

 

 

for ($Z = 0 ; $Z < count($nodelist) ; $Z++) {

    $sql = "SELECT * FROM filedata WHERE id = " . $nodelist["$Z"];

 

    if (!$resultx = $database->query($sql)) {

      die("Failure to retreive file node data");

    }

 

    $DataObj = $database->fetch_object($resultx);

    //$DataObj->filedata;

  echo $DataObj->filedata; //cannot be placed in object because of header

 

  }

 

 

 

 

 

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.