-1

I am retrieving an array of book objects with attributes like title,isbn and image. When I wish to display only labels using for loop it is displaying all entries. But when I try to display image, it only displays one. Also when I try to display both title and image, it says can

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/BookStore/BooksResult.php:14) in /Applications/XAMPP/xamppfiles/htdocs/BookStore/BooksResult.php on line 15.

Below is the code.

foreach($booksArr as &$book)
{
    $content = $book->Image;
    $title=$book->s_Title;                  
    echo $title;
    header('Content-type: image/jpg');
    {
        echo $content;
    }
}
Martin.
  • 10,494
  • 3
  • 42
  • 68
user1247412
  • 647
  • 7
  • 16
  • 29
  • You cannot send headers after you `echo` content to the page. – Evan Mulawski Mar 09 '12 at 20:03
  • I think you're approaching this wrong. Please show us an example output, and desired output/ – Madara's Ghost Mar 09 '12 at 20:03
  • 1
    Try seperate requests for the output and the image: generated alt – RumpRanger Mar 09 '12 at 20:07
  • possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – mario Mar 09 '12 at 22:08
  • possible duplicate of ["Warning: Cannot modify header information - headers already sent by" error](http://stackoverflow.com/questions/1912029/warning-cannot-modify-header-information-headers-already-sent-by-error) – outis Mar 10 '12 at 16:27

5 Answers5

0

Try adding

ob_start()

At the beggining of the file

EDIT: Also, only modify the header information once, just call the header function once at the beggining of the file, after ob_start()

gosukiwi
  • 1,569
  • 1
  • 27
  • 44
0

Headers may only be sent before any output is sent. You are echoing the title and then try to set the headers.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • What are you exactly trying to do? – Madara's Ghost Mar 09 '12 at 20:11
  • i am trying to display available book along with its images. Images are stored as blob in mysql – user1247412 Mar 09 '12 at 20:13
  • Images should be stored as images in the file system. Not as blobs in the database. Displaying images is used with the `` HTML tag. I think someone has some reading to do. – Madara's Ghost Mar 09 '12 at 20:15
  • why can't images be stored as blob in mysql? – user1247412 Mar 09 '12 at 20:17
  • Ok. But as a part of project i need to use blob – user1247412 Mar 09 '12 at 20:20
  • Use the file system. Link to the image from the database. – Madara's Ghost Mar 09 '12 at 20:20
  • @user1247412: If you are unable to use the filesystem to store your images, look at my (updated) answer for an alternate method of fetching your images from the database. – George Cummins Mar 09 '12 at 20:36
  • 1
    @Truth: Storing images in filesystem directly instead of storing them in the database is not always an option. So answering to your "_I think someone has some reading to do_": I believe someone needs to learn about the situations where you cannot (or shouldn't) store the images in the filesystem. If you would put that comment within your answer, you certainly wouldn't get my upvote (and now my vote is locked, unfortunately). – Tadeck Mar 09 '12 at 20:52
0

The "Cannot modify header information..." problem is here:

header('Content-type: image/jpg');

Your PHP script is outputting HTML. You cannot change the content type of the output mid-stream. Instead, you should output an <img> tag that points to the image location.

UPDATE BASED ON COMMENT DATA:

If you need to serve your images as blobs from the database, you will need to create a separate script to fetch the images. First modify this one to reference the new script by creating an <img> tag with a src attribute that contains the path to your image-fetching script and the image ID:

<img src="/path/to/your/new/script.php?id=<?php echo $row_id; ?>" />

Substitute $row_id with the field data that uniquely identifies a book in your database.

Next, create the new image fetching script. It will contain three elements:

  1. A line to get the id from the GET request: $id = $_GET['id'];

  2. An SQL statement to fetch the image blob based on the ID. (Remember to sanitize your input!)

  3. A header() statement like the one you used in your original script

After the header() line, just output the blob, and your image will be displayed.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • I added echo ''; to the code. When I run it i simply get blank boxes. When I tried running simply the url with hardcoded id, i am able to get the image. What mistake am i doing here? – user1247412 Mar 09 '12 at 21:11
  • It looks like you are attempting to nest your PHP statements. Try this instead: `echo ' – George Cummins Mar 09 '12 at 21:13
  • Any idea about how an image can be resized before displaying ? – user1247412 Mar 09 '12 at 21:17
  • There are a few options: 1. You can use gd or ImageMagick in Retrieve.php to process the images on the fly, 2. You can at height and width attributes to your `` tags, or 3. You can run a separate script that processes all of the images in your database and resizes them as a batch. – George Cummins Mar 09 '12 at 21:20
0

You are trying to send all the photos in one request. HTML doesn't work like that. You need to do something like this:

foreach($booksArr as &$book) {
  $title=$book->s_Title;                  
  echo $title;
  echo '<img src="book_image.php?id=' . $book->id . '" alt="' . $title . '"/>';
}

and then handle the books in a separate request on book_image.php, where you would print out the appropriate header and content, something like this:

$id = $_GET['id'];
// get the book by id from db
header('Content-type: image/jpg');
echo $book->Image;
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • You are incorrect about HTML not working like that. Of course you can output multiple images on the same page, without the need to separate them into another requests. In other words you do not need separate file for returning images. – Tadeck Mar 09 '12 at 20:10
  • You can do it all in one request by doing a Data URL, however it's a bit of an advanced technique, it increases the bandwidth by ~33%, and is clearly not what OP is doing. That's the only way I know of by which you can have the HTML and the image data in the same request. If you know another, please elaborate. – Amadan Mar 09 '12 at 20:23
  • What OP does is clearly incorrect, but what exactly OP wants to achieve is still unknown. Data URLs have some disadvantages, but they are great for many different things. In this case your solution has a potential of increasing the number of requests by `n * 100%` (where `n` is the number of images, percentage is added for clarity), same for database calls. Depending on what is more important (number of db calls and number of requests or the amount of transfer used for representing images), the solution is different. But your "_HTML doesn't work like that_" remains incorrect. – Tadeck Mar 09 '12 at 20:58
0

If you really want to understand this warning, and you want to work more with PHP in the future then you should start to learn how the HTTP protocol works. Its fun, and you will see much more clearly after:)

(this is just a general advice, not the exact answer to your question)

Filkor
  • 642
  • 6
  • 18