retreiving image from database(blob)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    retreiving image from database(blob)

    hii,
    i have stored the images from form to database in blob format.now i have a necessity to retrieve image from database and store it in a folder ..how to do this..


    thanks,
    Pradeep
  • rohypnol
    New Member
    • Dec 2007
    • 54

    #2
    Hi, I've found this to be very helpful: http://www.phpriot.com/articles/images-in-mysql
    It shows how to display the image on page 8. Hope it helps :o)
    If you still need help, post the structure if your table and how you'd like to show the images to the user (ie, <img src="img.php?id =..."> or direct url or what way you'd like to use).

    Regards,
    Tom

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      i hve used that script .....but that doesnot work for me bcos i wanted to mail the picture which i am not able to do .so i that of saving the picture fron database to some jpg file and then mailing it....

      i am sending the mailing script chk if u can help
      [PHP]<?php
      mysql_connect(" localhost","roo t","12233");
      mysql_select_db ("xyz");

      $str_sql="selec t * from pphp where id=184";
      //'".$_GET['Id']."'";
      $Colloquiums1 = array();
      $i=0;

      $res_id=mysql_q uery($str_sql);
      while ($sub_row=mysql _fetch_array($r es_id)) {
      $tmp1 = array(
      'Count' => $i+1,
      'ID' => $sub_row['ID'],
      'pic' => $sub_row['pic']
      )
      $Colloquiums1[$i++] = $tmp1;
      }
      $ID=$Colloquium s1[0]['ID'];
      $nid=$ID;
      $name=strtouppe r($Colloquiums1[0]['name']);
      $picname=$Collo quiums1[0]['picname'];
      $address=$Collo quiums1[0]['address'];
      //$pic=print("<im g src='/phdstudents/admitcardpic.ph p?Id=184'>");
      // multiple recipients
      //$to = 'aidan@example. com' . ', '; // note the comma
      //$to .= '<REMOVED-BY-MOD>@gmail.com' ;

      // subject
      $subject = 'admitcard';

      // message
      $message = '
      <html>
      <head>
      <title>Birthd ay Reminders for August</title>
      </head>
      <body>
      <div id="textcontent ">
      <h3>Admit Card</h3>
      <table align="center" width="100%" border="1" cellspacing="2" cellpadding="2" id="img1">
      <tr>
      <td align="left">Re f=' . $ID .'</td>
      </tr>
      </table>
      <table align="center" width="100%" border="0" cellspacing="2" cellpadding="2" id="img1">
      <tr>
      <td>Name</td>
      <td>:</td>
      <td><b>' . $name . '</b></td>
      /*problem here in mailing the pic*/
      <td colspan=2>' . <img src="http://abc.com/abc/pic.php?Id=190" > . '</img></td>
      </tr>

      <tr>
      <td>Address</td>
      <td>:</td>
      <td><b>' . $address . '</b></td>
      </tr>
      </table><table>Th is is a automated email(admitcard ) generated.Copyr ight@IIA.</table>
      </body>
      </html> ';

      // To send HTML mail, the Content-type header must be set
      $headers = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

      // Additional headers
      $headers .= 'To: <REMOVED-BY_MOD>@gmail.c om, Kelly <kelly@example. com>' . "\r\n";
      $headers .= 'From: webmaster <webmaster@iiap .res.in>' . "\r\n";
      $headers .= 'Cc: testcom' . "\r\n";
      $headers .= 'Bcc: cc@example.com' . "\r\n";

      // Mail it
      $ok=mail($to, $subject, $message, $headers);
      if($ok){
      echo "mail sent";
      }
      else{
      echo "fail";
      }

      ?>
      [/PHP]

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Hello Pradeep,
        I think you you can do this by adding one more header to your mail script.
        [code=php]
        $header .= "Content-Type: application/octet-stream; name=\"".$filen ame."\"\r\n";[/code]

        Now we need the real file name as the attachment, Let me say, I never tried to take it directly from MySQL blob and put it in to header.But I think we can do it( Guys, correct me if I'm wrong here). Why don't you give it a try, until I also do some coding for this.

        Accideblty you have added a email address to the script and I am assuming that was your personal email. for your own safety and also because of the rules of our forum I had to remove it.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          I'm not sure this is the best way to send images in emails, but you can "embed" images into HTML by using a data URL scheme as the src.

          Like, for example:
          [code=php]
          <?php
          // Fetch the image. Could be replaced with your database data
          $filename = "img.jpg";
          $data = file_get_conten ts($filename);

          // Encode the data and send it using an <img> tag
          $data = base64_encode($ data);
          echo '<img src="data:image/jpeg;base64,', $data, '" alt="testing... " />';
          ?>
          [/code]
          A modern browser should display this just like a normal <img> tag using the file name as a src.

          Comment

          • ak1dnar
            Recognized Expert Top Contributor
            • Jan 2007
            • 1584

            #6
            Originally posted by Atli
            I'm not sure this is the best way to send images in emails, but you can "embed" images into HTML by using a data URL scheme as the src.

            Like, for example:
            [code=php]
            <?php
            // Fetch the image. Could be replaced with your database data
            $filename = "img.jpg";
            $data = file_get_conten ts($filename);

            // Encode the data and send it using an <img> tag
            $data = base64_encode($ data);
            echo '<img src="data:image/jpeg;base64,', $data, '" alt="testing... " />';
            ?>
            [/code]
            A modern browser should display this just like a normal <img> tag using the file name as a src.
            Yes, I am just talking about mail with file attachments here. May be the OP is asking about sending inline images with his email. but original post doesn't say anything about it.

            Comment

            • pradeepjain
              Contributor
              • Jul 2007
              • 563

              #7
              hey guys i got thse solution i cld mail picture by using

              <img src="http://www.abc.com/pic.php?Id='. $nid .'"></img>



              Thanks,
              Pradeep

              Comment

              • rohypnol
                New Member
                • Dec 2007
                • 54

                #8
                Originally posted by pradeepjain
                hey guys i got thse solution i cld mail picture by using

                <img src="http://www.abc.com/pic.php?Id='. $nid .'"></img>



                Thanks,
                Pradeep
                Hello,

                That's not a solution, that's a work-around. To be honest, I don't know the solution to what you need, but you should look into MIME. There are PHP classes out there that support MIME attachments (try looking on google) and you can include images in email messages. Using an <img> tag doesn't help very much because almost all email clients block images and the user has to click on something to unblock them and allow them to load which may not always be acceptable. Also, if someone is saving that email on their computer they might not get the image from some clients, they'll only get the html with the tag so if they'll be disconnected from the Internet or if you decide to change the URL by which the image is accessed, they won't see the image. Another thing to take into account is bandwidth. Every time someone will look at an email, their client will connect to your web server to check if the image has updated and some clients might even re-download the image every time.

                The <img> be OK for what you need, but I thought you might want to consider the things from above and decide if you need to use MIME.

                Regards,
                Tom

                Comment

                Working...