4 Replies Latest reply on Jul 25, 2006 5:01 AM by memory_imprint

    Displaying images stored in SQL Server

    Impressed with Flex
      Is there someway of displaying images stored within SQL Server. Is FDS required? Prefer a method to display images without using FDS; Web Service would be okay if that can work
        • 1. Re: Displaying images stored in SQL Server
          inlineblue Level 1
          We had to do this in our project and I think the simplest thing to do is to offload the work to the server. Have your back-end server load the binary data from the DB and then send it back as an image (content type image/xxx). This way, you can access the image using a simple URL. In Flex, you can just use the Loader.
          • 2. Re: Displaying images stored in SQL Server
            Impressed with Flex Level 1
            Did you have to save the images as files & then refer to them in the Url & did you stream the image without saving - if streamed what Url did you use?
            • 3. Re: Displaying images stored in SQL Server
              I do that sort of thing using PHP and mySQL the binary part of the image is stored in a BLOB field (Binary Large OBject) along the rest of the information necessary for the http headers (e.g. mime type, file name, file size) being staored in their own fields.

              That info is then used to build the file using PHP. The PHP file contains a mySQL query whos result is echoed with the appropriate headers. The URL points at the PHP file rather than at any saved image.

              Here's the php:
              # display_imagebank_file.php
              # Note: the ID is passed through the url e.g.
              # this_files_name.php?id=1

              # connect to mysql database
              mysql_connect('HOST', 'USERNAME', 'PASSWORD');

              # run a query to get the file information
              $query = mysql_query("SELECT FileName, MimeType, FileSize, FileContents FROM blobTable WHERE ID='$id'");

              # perform an error check
              $fileName = mysql_result($query,0,0);
              $fileType = mysql_result($query,0,1);
              $fileSize = mysql_result($query,0,2);
              $fileContents = mysql_result($query,0,3);
              header("Content-type: $fileType");
              header("Content-length: $fileSize");
              header("Content-Disposition: inline; filename=$fileName");
              header("Content-Description: from imagebank");
              header("Connection: close");
              echo $fileContents;

              $numRows = mysql_num_rows($query);
              echo "File not found <br>";
              echo $numRows;
              echo " is the number of rows returned for id = ";
              echo $ID;


              Hope that helps

              • 4. Re: Displaying images stored in SQL Server

                some tipps to add more security to your script:

                1. Your script needs that register_globals is activated on the webserver. If you can, you should deactivate that option because it is possible to overwrite variables by adding the same as GET-Parameters.

                2. If you use GET-Parameters in MySQL Queries be sure that you have validated the value for that variable. Well, when you transfer ID's by GET-Paramters you can validate them easy by using the is_numeric() function.

                here is an example:

                if(is_numeric($_GET['id'])) {
                // MySQL QUERY here
                } else {
                die('GET-Parameter is not valid');