Announcement

Collapse
No announcement yet.

Loading Picture

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loading Picture

    Hey all,

    I am quite new to PHP and MySQL. Just wondering, can I save all types of pictures files into my MySQL DB? And how do I load it? Can anyone lead me in coding. Please help! Thanks

  • #2
    as far as i know mySQL only lets you save txt to it
    Cons

    Comment


    • #3
      Yes, you can save images into a mySQL database.
      Code:
      // Read the image file into $img
      $fp = @fopen ($graphic, "r");
      if ($fp){
          $photoBlob = fread ($fp, filesize($graphic));
          $img = str_replace ("'", "''", $img);
          $img = str_replace ("\\", "\\\\", $img);
          $img = str_replace ("\"", "\\\"", $img);
          $img = str_replace ("\0", "\\\0", $img);
          fclose ($fp);
      }
      // Now save $img into a DB field (most likely a blob data type)
      To print the image, you need a seperate file. Such as img_print.php
      Call it like this:
      Code:
      <img src="img_print.php?id=123">
      img_print.php looks like this:
      PHP Code:
      // Read image from DB
      $query "SELECT image_field_name FROM table_name WHERE ID=$id";
      $result mysql_query($query);
      if (!
      $result) {
          echo 
      "\n<!--QUERY ERROR:" mysql_error() . "-->\n";
          echo 
      "\n<!--QUERY:$query-->\n";
      }
      $tmp mysql_fetch_row($result);

      // Now pring the image
      echo $tmp[0]; 

      Comment


      • #4
        Remember... everything boils down to 1's and 0's.

        Comment


        • #5
          so what I said was correct coz you are in essence simply storing as a txt file
          Cons

          Comment


          • #6
            Well...
            The binary data is being displayed as a bunch of (non-ASCII) characters.

            Open a graphic in notepad (or any other ASCII editor). You're viewing binary data in a non-binaray form. But that doesn't mean it's text.

            Comment


            • #7
              I get how the binary is stored in the txt file and all but I can't figure the PHP script out. What does it mean?

              Comment


              • #8
                check out this thread: http://www.vbulletin.com/forum/showt...threadid=19186

                Comment


                • #9
                  In my experience, I've never had to print a head before the image's binary data. It just seemed to work.

                  If you open a GIF in notepad, the first few characters say "GIF89a". If you open a JPEG, it has all kinds of stuff in the first few lines. I think browsers are smart enough to read this info, and determine the image type.

                  Comment


                  • #10
                    hmm
                    well i suppose it cant really hurt too much either

                    Comment


                    • #11
                      nope... better to be safe then sorry.

                      Comment


                      • #12
                        Originally posted by Chexbox
                        I get how the binary is stored in the txt file and all but I can't figure the PHP script out. What does it mean?
                        Yeah, that helps. But I wanted to be able to understand the code because, like most scripts on these forums, there are some complex things in there that are not explained and that I can't understand and people just don't have the time or patients to explain things to those that don't understand. I'll just get a book on PHP.

                        Comment


                        • #13
                          ask specific questions about it

                          Comment


                          • #14
                            I'm sorry
                            okay, what happens to the variables when a new page is loaded within the same browser window?

                            Comment


                            • #15
                              theyre passed to the script and processed, thats pretty much the whole basis of the scripts....

                              Comment

                              widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                              Working...
                              X