Retreive images from DB/File

Hey there :blush:

I want to retrieve images that will be uploaded from one file(into DB) and place them into a photo gallery. Here is my code:

<div class="py-5">
  <?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);     
$mysqli = new mysqli("db5004526329.hosting-data.io", "dbu1026002", "meka%1981", "dbs3781610");
$query = "SELECT image FROM tbl_member ORDER BY ID DESC";
$result = $mysqli->query($query);

while($row = $result->fetch_array(MYSQLI_ASSOC) ) {
    printf("%s (%s)\n", $row["image"]); 
?>
    <div class="container">
      <div class="row hidden-md-up">
        <div class="col-md-4">
          <div class="card">
            <div class="card-block">
            </div>
          </div>
        </div>
        <div class="col-md-4">
          <div class="card">
            <div class="card-block">
            </div>
          </div>
        </div>
        <div class="col-md-4">
          <div class="card">
            <div class="card-block">
            </div>
          </div>
        </div>
      </div><br>
      <div class="row">
        <div class="col-md-4">
          <div class="card">
            <div class="card-block">
            </div>
          </div>
        </div>
        <div class="col-md-4">
          <div class="card">
            <div class="card-block">
            </div>
          </div>
        </div>
        <div class="col-md-4">
          <div class="card">
            </div>
          </div>
        </div>
      </div>
    </div>
<?PHP
}
?>

Is this correct…want to make sure?

Thank ya :yellow_heart:

Normally, you never store images inside a database. It is not a good practice. Images must be stored in a certain manner if you break the rules and do so. How were the images stored into the database?

Normally, you would save the image in a folder and save the path and filename to the image into the database. In this manner, you can retrieve the path/filename and just load it as an < img > tag. Otherwise, you need to format the image in complicated format and reconvert it back before displaying.

Now, with all that said… How was the images stored in the database? What format is the field? ( BLOB huge binary, ??? ) More info will help us help you.

1 Like

Hey @ErnieAlex :blush:

I took on an already made script…goal is to have images uploaded within home page form a gallery for the user( think of FB…each member has their own pic gallery created from what was uploaded either on the dash or profile page). I have an image COL in BLOB format within DB.
File upload code from Home page:

 <div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Upload Photo/Text</button>
  <div id="myDropdown" class="dropdown-content">
     <form action="upcontent.php" method="post" enctype="multipart/form-data">
  
  <input type="file" name="fileToUpload" id="fileToUpload">
   <br><br>     
     Words: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
   <input type="submit" value="Submit" name="submit">
</form>
  </div>
</div> 

Code from gallery page

<div class='container'>
            <div class="gallery">
              
            <?php 
            // Image extensions
            $image_extensions = array("png","jpg","jpeg","gif");

            // Target directory
            $dir = 'img/';
            if (is_dir($dir)){
                
                if ($dh = opendir($dir)){
                    $count = 1;

                    // Read files
                    while (($file = readdir($dh)) !== false){

                        if($file != '' && $file != '.' && $file != '..'){
                            
                            // Thumbnail image path
                            $thumbnail_path = "img/thumbnail/".$file;

                            // Image path
                            $image_path = "img/".$file;
                            
                            $thumbnail_ext = pathinfo($thumbnail_path, PATHINFO_EXTENSION);
                            $image_ext = pathinfo($image_path, PATHINFO_EXTENSION);

                            // Check its not folder and it is image file
                            if(!is_dir($image_path) && 
                                in_array($thumbnail_ext,$image_extensions) && 
                                in_array($image_ext,$image_extensions)){
                                ?>

                                <!-- Image -->
                                <a href="<?php echo $image_path; ?>">
                                    <img src="<?php echo $thumbnail_path; ?>" alt="" title=""/>
                                </a>
                                <!-- --- -->
                                <?php

                                // Break
                                if( $count%4 == 0){
                                ?>
                                    <div class="clear"></div>
                                <?php    
                                }
                                $count++;
                            }
                        }
                            
                    }
                    closedir($dh);
                }
            }
            ?>
            </div>
        </div>

Well, remember that you do not normally save images in dabases. You just use a text field in the database table like a varchar(255) and you store the path/filename of the image in it. Then, when you want to display it, you use this field to place it inside an < img > tag as the src so that it displays. If you have problems, let us know and we can help.

1 Like

Just edited my response with code :blush:
Also made edit within DB to varchar

<img src="data:image/jpeg;base64,<?php echo base64_encode( $image_data ); ?>" />
1 Like

Hey @astonecipher :blush:

Thank ya so very much for this…it means alot :heart:
Question: You made a change to the img src but not the a href above it…would the data:image be placed there or take it out being that the gallery page id the only page that will showcase images other than the home page?

This is an example of how you show the image that’s stored in a database. Play with it to see how it can fit your needs.

I still agree that it’s not the best practice to do so, storage is cheap, but sometimes you have to work within the constraints you are given.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service