Certainly! Uploading images in PHP using PDO (PHP Data Objects) with an Object-Oriented Programming (OOP) approach involves handling the file upload and storing the file information in a database. Below is a simple example of PHP image upload using PDO and OOP:

  1. Create a Database Table: Let’s assume you have a table named images with columns id, filename, and filepath.
   CREATE TABLE images (
       filename VARCHAR(255) NOT NULL,
       filepath VARCHAR(255) NOT NULL
  1. Create a PDO Connection Class:
   class Database {
       private $host = 'your_host';
       private $user = 'your_username';
       private $password = 'your_password';
       private $dbname = 'your_database';
       private $conn;

       public function __construct() {
           $dsn = "mysql:host={$this->host};dbname={$this->dbname}";

           try {
               $this->conn = new PDO($dsn, $this->user, $this->password);
               $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
           } catch (PDOException $e) {
               echo "Connection failed: " . $e->getMessage();

       public function getConnection() {
           return $this->conn;
  1. Create an Image Upload Class:
   class ImageUploader {
       private $conn;

       public function __construct($conn) {
           $this->conn = $conn;

       public function upload($file) {
           $targetDir = "uploads/";
           $targetFile = $targetDir . basename($file['name']);
           $uploadOk = 1;
           $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));

           // Check if image file is a actual image or fake image
           $check = getimagesize($file['tmp_name']);
           if (!$check) {
               echo "File is not an image.";
               $uploadOk = 0;

           // Check if file already exists
           if (file_exists($targetFile)) {
               echo "Sorry, file already exists.";
               $uploadOk = 0;

           // Check file size
           if ($file['size'] > 5000000) {
               echo "Sorry, your file is too large.";
               $uploadOk = 0;

           // Allow certain file formats
           if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif") {
               echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
               $uploadOk = 0;

           // Check if $uploadOk is set to 0 by an error
           if ($uploadOk == 0) {
               echo "Sorry, your file was not uploaded.";
               return false;
           } else {
               // if everything is ok, try to upload file
               if (move_uploaded_file($file['tmp_name'], $targetFile)) {
                   // Insert image details into the database
                   $stmt = $this->conn->prepare("INSERT INTO images (filename, filepath) VALUES (:filename, :filepath)");
                   $stmt->bindParam(':filename', $file['name']);
                   $stmt->bindParam(':filepath', $targetFile);

                   return $stmt->execute();
               } else {
                   echo "Sorry, there was an error uploading your file.";
                   return false;
  1. Usage:
   // Include the classes
   require_once 'Database.php';
   require_once 'ImageUploader.php';

   // Create a database connection
   $db = new Database();
   $conn = $db->getConnection();

   // Create an image uploader object
   $imageUploader = new ImageUploader($conn);

   // Check if the form was submitted
   if ($_SERVER['REQUEST_METHOD'] === 'POST') {
       // Check if a file was selected for upload
       if (!empty($_FILES['file']['name'])) {
           // Attempt to upload the file and insert details into the database
           if ($imageUploader->upload($_FILES['file'])) {
               echo "File uploaded successfully.";
           } else {
               echo "File upload failed.";
       } else {
           echo "Please select a file for upload.";

   <!DOCTYPE html>
   <html lang="en">
       <meta charset="UTF-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0">
       <title>Image Upload</title>
       <form action="" method="post" enctype="multipart/form-data">
           <input type="file" name="file" accept="image/*">
           <input type="submit" value="Upload">

This is a basic example, and you may need to adjust it based on your specific requirements. Make sure to create the “uploads” directory in your project to store the uploaded images, and adjust the file paths accordingly. Additionally, consider implementing security measures, such as validating file types and using secure file naming conventions.

By admin