Image code modification

Hey☺️

Implemented a script and it works fine but want to tweak it so only one image is displayed after upload. As of now image is displayed after upload but also images previously uploaded (like a gallery). Goal is to have a singular profile image instead of a profile image gallery. Here is my code:

upload.php


<?php
// Include the database configuration file
require_once 'dbConfig.php';

// If file upload form is submitted
$status = $statusMsg = '';
if(isset($_POST["submit"])){
    $status = 'error';
    if(!empty($_FILES["image"]["name"])) {
        // Get file info
        $fileName = basename($_FILES["image"]["name"]);
        $fileType = pathinfo($fileName, PATHINFO_EXTENSION);
        
        // Allow certain file formats
        $allowTypes = array('jpg','png','jpeg','gif');
        if(in_array($fileType, $allowTypes)){
            $image = $_FILES['image']['tmp_name'];
            $imgContent = addslashes(file_get_contents($image));
        
            // Insert image content into database
            $insert = $db->query("INSERT into img (image, uploaded) VALUES ('$imgContent', NOW())");
            
            if($insert){
                $status = 'success';
                $statusMsg = "File uploaded successfully.";
            }else{
                $statusMsg = "File upload failed, please try again.";
            }
        }else{
            $statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to upload.';
        }
    }else{
        $statusMsg = 'Please select an image file to upload.';
    }
}

// Display status message
//echo $statusMsg;
?>

view.php


   
      <?php
// Include the database configuration file
require_once 'dbConfig.php';

// Get image data from database
$result = $db->query("SELECT image FROM img ORDER BY uploaded DESC");
?>
      <div class="container">
    <?php if($result->num_rows > 0){ ?>
        <div class="w3-container">
            <?php while($row = $result->fetch_assoc()){ ?>
                <img src="data:image/jpg;charset=utf8;base64,<?php echo base64_encode($row['image']); ?>" />
            <?php } ?>
        </div>
    <?php }else{ ?>
        <p class="status error">Image(s) not found...</p>
    <?php } ?>
</div>
     ```

 

Thank ya ❤️

 


​

And, what is the problem?

1 Like

@ErnieAlex

I thought I stated it: images after upload would show up as a gallery. What I wanted to create a profile pic upload.

I got figured out…added DESC query limit to 1 :blush:

Glad you solved it! Always nice to solve a programming problem…

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