I would like to download images stored as Blob in mysql. I do not want to just display the image on a web page. I want to want to download the image file onto my computer. How do I do this? Below is the script for displaying the image on a web page. How do I modify this?

<?php
$link=mysql_connect("localhost","root","");
if(!$link)
{
   die("could not connect:".mysql_error());
}
mysql_select_db("media",$link);
$que="select imageBlob from images where imageId=10"; //imageBlob- name of the blob data type field in mysql.
$ret=mysql_query($que)or die("Invalid query: " . mysql_error());
header("Content-type: image/jpeg");
echo mysql_result($ret, 0);
mysql_close($link);
?>

    I've found the following headers work with every browser I've tried them on:

    header('Content-Length: '.$fileSize);
    header('Content-Type: '.$mimeType);
    header('Content-Disposition: attachment; filename="'.$fileName.'"');
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    

    (You'll need to change/set the variables for the various values/names.)

      I do not want to display the images in the browser. I want to download the data from blob and store it as an image on my computer.

        So what you need to do, instead of sending the header and using echo, is to use the file functions to [man]fopen[/man] a new file and [man]fwrite[/man] the image data into it.

          I do not get what you say. Let me state the problem again. I have uploaded images into mysql as Blobs. I would now like to download them back again onto my system by specifying the value of the primary key. I have associated a primary key to the database using which i want to download the images. You are suggesting that I use fopen. How can I use fopen on my sql ? I am new to php so kindly excuse my ignorance !

            you use fopen to create the new file, fwrite to put the blob data in the file.

              I tried what you suggested. But I get the following error: fwrite() expects parameter 2 to be string, resource given

              <?php
              $link=mysql_connect("localhost","root","");
              if(!$link)
              {
              	die("could not connect:".mysql_error());
              }
              mysql_select_db("media",$link);
              $que="select imageBlob from images where imageId=10";
              $ret=mysql_query($que)or die("Invalid query: " . mysql_error());
              $file=fopen("downloadedImage.jpeg","w");
              fwrite($file,$ret);
              ?>
              

                $ret is a resource, after mysql_query() you will need to use mysql_result or mysql_fetch_*()

                  Thank you all. The code displayed below works.
                  @: I used mysql_result. I could not find out how to use mysql_fetch() ( denotes array, assoc, field, lengths, object, row)
                  Can any one suggest how to use mysql_fetch
                  *() instead of mysql_result. It is not required though but might be useful for some one else.

                  <?php
                  $link=mysql_connect("localhost","root","");
                  if(!$link)
                  {
                  	die("could not connect:".mysql_error());
                  }
                  mysql_select_db("media",$link);
                  $que="select imageBlob from images where imageId=21";
                  $ret=mysql_query($que)or die("Invalid query: " . mysql_error());
                  $returned=mysql_result($ret,0,0);
                  $file=fopen("downloadedImage.jpeg","w");
                  fwrite($file,$returned);
                  ?>
                  
                    moshy;10934565 wrote:

                    I do not want to display the images in the browser. I want to download the data from blob and store it as an image on my computer.

                    Which is what those headers will do: cause the browser to open up the download window so you can select where to save it on your computer, instead of displaying it.

                      moshy;10934572 wrote:

                      Thank you all. The code displayed below works.
                      @: I used mysql_result. I could not find out how to use mysql_fetch() ( denotes array, assoc, field, lengths, object, row)
                      Can any one suggest how to use mysql_fetch
                      *() instead of mysql_result. It is not required though but might be useful for some one else.

                      The manual has better examples than ones i could ever think of.

                        NogDog;10934573 wrote:

                        Which is what those headers will do: cause the browser to open up the download window so you can select where to save it on your computer, instead of displaying it.

                        You are absolutely correct :quiet:

                          Write a Reply...