displaying images.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashraf02
    New Member
    • Feb 2008
    • 53

    displaying images.

    i used a code from a website that allows you to display images. however everything works fine from storing the image to the database but it does not display the image.

    the following code is the one i have used.

    Storing the images:

    [PHP]<HTML>
    <HEAD><TITLE>St ore binary data into SQL Database</TITLE></HEAD>
    <BODY>

    <?php

    if (isset($_REQUES T['submit'])) {

    mysql_connect(" localhost","roo t","");
    mysql_select_db ("binary_data") ;

    $data = addslashes(frea d(fopen($_FILES['form_data']['tmp_name'], "r"), $_FILES['form_data']['size']));

    $result= mysql_query("IN SERT INTO binary_data (description,bi n_data,filename ,filesize,filet ype) ".
    "VALUES ('".$form_descr iption."','".$d ata."','".$_FIL ES['form_data']['name']."','".$_FIL ES['form_data']['size']."','".$_FIL ES['form_data']['type']."')");

    $id= mysql_insert_id ();
    print "<p>This file has the following Database ID: <b>$id</b>";

    mysql_close();

    } else {
    ?>
    <form method="post" action="store.p hp" enctype="multip art/form-data">
    File Description:<br >
    <input type="text" name="form_desc ription" size="40">
    <INPUT TYPE="hidden" name="MAX_FILE_ SIZE" value="1000000" >
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data ">
    <p><input type="submit" name="submit" value="submit">
    </form>

    <?php

    }

    ?>[/PHP]

    Getting the image from database :

    [PHP]<?php

    if($id) {

    mysql_connect(" localhost","roo t","");

    mysql_select_db ("binary_data") ;

    $query = "select bin_data,filety pe from table where id='$id'";
    $result = mysql_query($qu ery);

    $data = mysql_result($r esult,0,"bin_da ta");
    $type = mysql_result($r esult,0,"filety pe");

    header( "Content-type: $type");
    header( "Content-type: image/pjpeg");
    echo $data;


    };
    ?>[/PHP]

    displaying the image:

    [PHP]<html>
    <head>
    <title>images retrieved from database</title>
    </head>

    <body>

    <img src="getdata.ph p?id=3">

    </body>
    </html>[/PHP]

    if someone can help i would greatly appreciate it.

    thanks

    ashraf
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Open getdata.php?id= 3 in your browser.
    See what error it gives.

    It should be $_GET['id'] instead of $id

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by hsriat
      Open getdata.php?id= 3 in your browser.
      See what error it gives.

      It should be $_GET['id'] instead of $id
      More over, should be:
      [php]
      $id = $_GET['id']; # do some cleaning of this.
      [/php]
      You should, also, sanitize that input; it could be malicious.

      Regards.

      Comment

      • ashraf02
        New Member
        • Feb 2008
        • 53

        #4
        thanks for ur reply guys

        but still no luck i'v tried both ways. i'm new to php and i have an assignment to hand in soon. anymore suggestion.

        do u think storing the url to the images would be easier?

        thanks in advance.

        ashraf

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          Originally posted by ashraf02
          thanks for ur reply guys

          but still no luck i'v tried both ways. i'm new to php and i have an assignment to hand in soon. anymore suggestion.

          do u think storing the url to the images would be easier?

          thanks in advance.

          ashraf
          What error do you get when you open getdata.php?id= 3 in your browser?

          Comment

          • ashraf02
            New Member
            • Feb 2008
            • 53

            #6
            i get no error just a blank screen

            Comment

            • ashraf02
              New Member
              • Feb 2008
              • 53

              #7
              Originally posted by hsriat
              What error do you get when you open getdata.php?id= 3 in your browser?
              sorry i only put in getdata.php not getdata.php?id= 3

              this is the following erros i get on the page.


              Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\get data.php on line 13

              Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\wamp\www\get data.php on line 14

              Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\get data.php:13) in C:\wamp\www\get data.php on line 16

              Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\get data.php:13) in C:\wamp\www\get data.php on line 17

              Comment

              • ashraf02
                New Member
                • Feb 2008
                • 53

                #8
                i've sorted the problem out. i looked at the errors and figured out wat was wrong in mysql_query it said select from table instead of the actual table name.

                thanks anyway i wouldn't have found that out if u didn't tell me to look at the errors on that particular script. nice one "hsriat" and "markusn00b "

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Try using a while loop to assign the data:
                  [php]
                  $_res = mysql_query("SE LECT .... ");
                  while($_rows = mysql_fetch_arr ay($_res))
                  {
                  $_data = $_rows['bin_data'];
                  $_file = $_rows['filetype'];
                  }
                  [/PHP]

                  Regards.

                  EDIT: Neglect this then.

                  Glad you've sorted it!

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Do not post duplicate threads in this forum! I does not help you in any way. It just annoys people.

                    The duplicate thread has been removed.

                    moderator

                    Comment

                    • ashraf02
                      New Member
                      • Feb 2008
                      • 53

                      #11
                      i didn't duplicate the thread. next time b4 u delete a post read the code and maybe compare the two codes and check. this code was for BLOB files and the other was for linking url files.

                      Comment

                      • hsriat
                        Recognized Expert Top Contributor
                        • Jan 2008
                        • 1653

                        #12
                        Originally posted by ashraf02
                        i didn't duplicate the thread. next time b4 u delete a post read the code and maybe compare the two codes and check. this code was for BLOB files and the other was for linking url files.
                        In the code you provided in the other thread (removed one), view source of your page in a browser (recommended FF), and see what is the problem with your img tag's src attribute.
                        Check if the referred source actually exists.

                        Comment

                        • ashraf02
                          New Member
                          • Feb 2008
                          • 53

                          #13
                          thanx "HSRIAT" but i have sorted the problem out i did wat u said and it seemed that i forgot to put a forward slash in the code thanks any way much appreciated.

                          Comment

                          • Markus
                            Recognized Expert Expert
                            • Jun 2007
                            • 6092

                            #14
                            Originally posted by ashraf02
                            i didn't duplicate the thread. next time b4 u delete a post read the code and maybe compare the two codes and check. this code was for BLOB files and the other was for linking url files.
                            You DID duplicate the thread.

                            I read the new thread, fully, and it was in no-way different from this thread.

                            Even it a question was partly linked to this thread, you should keep it on the same thread.

                            Regards,

                            Comment

                            • hsriat
                              Recognized Expert Top Contributor
                              • Jan 2008
                              • 1653

                              #15
                              Originally posted by markusn00b
                              You DID duplicate the thread.

                              I read the new thread, fully, and it was in no-way different from this thread.

                              Even it a question was partly linked to this thread, you should keep it on the same thread.

                              Regards,
                              It was a bit different. In that one, he was saving the name of the image in the db, and in this one, image is saved in db.

                              Though I'm sorry for argument.

                              :)
                              Harpreet

                              Comment

                              Working...