We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18367
    • 834 Posts
    I’ve got eform/eform2db storing uploaded images and files to a db, but how do I get them back out of the db for display?

    I can’t see any file paths anywhere in the db, just the binary data.

      Content Creator and Copywriter
      • 7231
      • 4,205 Posts
      You are uploading the images into the actual database? I usually store the paths and upload the images to the server. If you are storing the image to the database you need to make sure that the db field is set to type ’blob’.

      Found this article on storing images in the database: http://www.phpriot.com/articles/images-in-mysql
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 18367
        • 834 Posts
        Sorry for the confusion, let me explain a bit more.

        I’m not storing them to the db, eform2db is.

        They are a part of a user form that uploads a user’s details, plus company logos, brochures etc.

        And they are already in BLOB.

        Storing them isn’t the problem.

        It’s getting at them once they are in the db.

        Not being a coder I’m not sure what script or command to use to display them on a webpage.

        I can get all the other info displayed, plus the image file name
        ajax search instructions.txt or search_ajs.jpg
        but there’s no link to the file or actual image.

        See the screenshot for what it looks like
          Content Creator and Copywriter
          • 30223
          • 1,010 Posts
          but there’s no link to the file or actual image.

          That’s because the image does not exist anywhere on your file system. You have stored the actual image data into the database.

          To display images and other binary files that are stored in a database you’ll need a separate script (accessible via a url) that retrieves the binary data and sends it to the browser with the correct mime type. (The article dev_cw refers to discusses this on page 6)

          To display the image in web page you would then for example have something like this
          <img src="/showimage.php?img=recordid" alt="my image"/>


          where showimage.php is the script that you’d need to write which retrieves the binary data form the DB.
            • 18367
            • 834 Posts
            You have stored the actual image data into the database.

            Yes I know, sorry if I didn’t make that clear in the original post.

            I read the article (great resource link by the way) and understand the concepts, if not totally all the execution.

            But anyway as I see it, eform/eform2db isn’t actually giving the images/files a unique id. At least not one I can see in the db.

            Do I have to add extra instructions to the original eform/eform2db snippet?

            Thanks
              Content Creator and Copywriter
              • 30223
              • 1,010 Posts
              But anyway as I see it, eform/eform2db isn’t actually giving the images/files a unique id. At least not one I can see in the db.

              Do I have to add extra instructions to the original eform/eform2db snippet?

              It depends. As long as you have a unique value in each database row, such as an auto_increment field it should be easy to retrieve each image. If not, I’d change the DB table and insert one. Your display script simply needs to find the correct DB row. So suppose your table has a auto increment field called rec_id you would use an image url similar to showimage.php?img=122, then in your script retrieve the image data using SQL "SELECT image_field FROM table_name WHERE rec_id={$_POST[’img’]}"



                • 18367
                • 834 Posts
                Thanks TobyL,

                As long as you have a unique value in each database row, such as an auto_increment field

                Yes, I wondered if that might be the case. Yes there is an auto incremented data record id.

                However, I guess I am being a little ambitious.

                As you may have guessed from the screenshot I’m calling all of the data from the db to display/ list in a a table.

                There are about 22 fields in the db. Everything is being called ok except the image.

                At the moment the code to call the image file is
                <?php echo $row_List_2['brochure']; ?>
                which obviously just calls the image name as you can see in the screenshot. First row, last column.

                IF I understand you correctly, I need to replace that line with a call to another script to display the image.

                Also, as I’m listing the images, how do i get around not specifying an image id?
                  Content Creator and Copywriter
                  • 30223
                  • 1,010 Posts
                  IF I understand you correctly, I need to replace that line with a call to another script to display the image.

                  Exactly..

                  Also, as I’m listing the images, how do i get around not specifying an image id?

                  I’m afraid there isn’t.
                    • 18367
                    • 834 Posts
                    OK,

                    from what I’ve read elsewhere storing images on a db is not really a good idea.

                    It seems the best approach would be to modify the whole process to upload the images separately to the file system, and then call them back via another script.

                    That’ll be a challenge for me.

                      Content Creator and Copywriter