Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Displaying Images from a Database

    I have followed a tutorial on how to do this shown here. http://www.techsupportforum.com/foru...se-176804.html

    The images are storing in the database perfectly but I am having trouble with step 5 when you create a page to display the stored image. When I include the 3 header lines of code

    PHP Code:
    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type"); 
    I'm given these three errors:

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/36/7939036/html/instride.info/connect.php:9) in /home/content/36/7939036/html/instride.info/getpicture.php on line 19

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/36/7939036/html/instride.info/connect.php:9) in /home/content/36/7939036/html/instride.info/getpicture.php on line 20

    Warning: Cannot modify header information - headers already sent by (output started at /home/content/36/7939036/html/instride.info/connect.php:9) in /home/content/36/7939036/html/instride.info/getpicture.php on line 21

    And a million lines of gibberish below which I believe is the image. You can see this here http://instride.info/getpicture.php?fid=2

    When I comment the 3 header lines of code within the program making them ineffective, the three warnings do not show up. Just the same gibberish. If anyone has an idea of how I should modify this getpicture.php file so that this is successful I would really appreciate it. All of the code I used is exactly the same as the code you can see on that website. I did not deviate from it whatsoever.
    Last edited by ccarrin2; 02-17-2012 at 08:47 PM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    The only header I would be a biot skeptical about is the attachment header..

    I think you may have whitespace in there somewhere. Any whitespace, html, even a return/line feed will cause the headers to be sent to the browser. One the headers and a tiny bit of content are sent, you can't send any more headers.

    It would help if you could post the code but in the meantime, take a look at the link in my signature about the headers error.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thank you for your reply, what do you mean by whitespace. And my code can be viewed within the link I provided.
    Last edited by ccarrin2; 02-17-2012 at 08:48 PM.

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    There must be some whitespace somewhere, or even an error hiding in the source?

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire View Post
    There must be some whitespace somewhere, or even an error hiding in the source?
    This is the entire code I have for my getpicture.php - What do you mean by whitespace?

    PHP Code:
    <?
    if(isset($_GET['fid']))
    {
    // connect to the database
    include "connect.php";

    // query the server for the picture
    $fid $_GET['fid'];
    $query "SELECT * FROM files WHERE fid = '$fid'";
    $result  mysql_query($query) or die(mysql_error());

    // define results into variables
    $name=mysql_result($result,0,"name");
    $size=mysql_result($result,0,"size");
    $type=mysql_result($result,0,"type");
    $content=mysql_result($result,0,"content");

    // give our picture the proper headers...otherwise our page will be confused
    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    echo 
    $content;

    mysql_close();
    }else{
    die(
    "No file ID given...");
    }

    ?> 

    <img src="getpicture.php?fid=1">

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    Quote Originally Posted by ccarrin2 View Post
    This is the entire code I have for my getpicture.php - What do you mean by whitespace?
    Exactly that - white space:

    Code:
      <!--White space before this comment-->
    <html>
    </html>
    Same goes with PHP

    PHP Code:
      <!--More white space before the < character-->
    <?php
    ?>
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I apologize but I'm not getting what you mean. I compressed all of my php code, and removed all of this white space. Now it is all one line of code.

    PHP Code:
    <?php if(isset($_GET['fid'])){include "connect.php";$fid $_GET['fid'];$query "SELECT * FROM files WHERE fid = '$fid'";$result  mysql_query($query) or die(mysql_error());$name=mysql_result($result,0,"name");$size=mysql_result($result,0,"size");$type=mysql_result($result,0,"type");$content=mysql_result($result,0,"content");header("Content-Disposition: attachment; filename=$name");header("Content-length: $size");header("Content-type: $type");echo $content;mysql_close();}else{die("No file ID given...");}?> <img src="getpicture.php?fid=1">
    My result was this http://instride.info/getpicture.php?fid=3

  • #8
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You have either errors on connect.php or whitespace in there.

    headers already sent by (output started at /home/content/36/7939036/html/instride.info/connect.php:9)

  • #9
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire View Post
    You have either errors on connect.php or whitespace in there.

    headers already sent by (output started at /home/content/36/7939036/html/instride.info/connect.php:9)
    First off, thank you for even caring about my problem. I am pretty sure I do not have errors in my connect.php file b/c the pictures are inserting into the rows of my table perfectly so the connection is sound. I followed the directions that were posted through that link perfectly but it was written 5 years ago. Do you think it used a different version of php and maybe I need to tweak my code some how for that? I have no experience retrieving data from a database so I'm sorry if this is a stupid question.

  • #10
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    Quote Originally Posted by ccarrin2 View Post
    I apologize but I'm not getting what you mean.
    As I demonstrated, ANY NON VISBLE SPACES that are output within the HTML source are called WHITESPACES. That is because you cannot see them yet they are there.

    If you look at my examples again, before each comment you will see that the comment is indented with 2 space characters. THAT is whitespace and because it is sent before any other call to the header() function, it will trigger the reply headers to be sent.

    As I said previously, please read the link in my signature about this problem. I have written a very detailed explanation of it all.

    As for your PHP, there is no need to put it all on one line like that. Break it all up again onto seperate lines. As I demonstrated in my two samples, it is the OUTPUT to the browser that causes headers to be sent - not spaces with php code which php itself ignores and does not put out into the source.

    To put it bluntly, if you have an empty line above your <?php tag that will cause the headers to be sent.

    Please will you actually post us the code for connect.php? - You keep quoting that its this file thats causing the headers to be sent yet you're holding this file back.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #11
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    connect.php - There is no code above the opening php tag. I just copy and pasted the code from the other forum and entered in my information.

    PHP Code:
    <?php
    $server 
    "50.53.527.48";
    $username "username";
    $password "password";
    $database "database";
    mysql_connect("$server""$username""$password") or die(mysql_error());
    mysql_select_db("$database") or die(mysql_error());
    ?>
    Here is the result with this code. http://instride.info/getpicture.php?fid=5

  • #12
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    Highlight the code in your editor - I'll bet you'll find spaces at the end of that file somewhere.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    I was correct - you have whitespace somewhere. I've just visited your url with a http program that I use for purposes like this and sure enough, you have ONE SPACE being output somewhere - note the alignment between the first two lines:

    Last edited by tangoforce; 02-18-2012 at 01:52 PM.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    ccarrin2 (02-18-2012)

  • #14
    New Coder
    Join Date
    Jan 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Wow! Thank you so much! Do you have a website so I can donate to you?

  • #15
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    Unfortunately no lol. Just the satisfaction of helping someone

    As I mentioned earlier, I suspect the whitespace is at the end of your connect.php so highlight the entire file (CTRL A) and I bet you'll see that one space at the end.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •