Upload images into SQL server database table using PHP code

186 Views Asked by At

I'm a beginner in working with php and sql server. I have been working on a php code which its functionality is upload an image in a sqlsrv table. At this moment my code can be able to save the image to the uploads folder, however, my code can be able to insert the image that is in that folder. I got the error of one my echo that is "Sorry, there was an error uploading your file." I don't know if it is necessary to use Ole Automation Procedures. P.S. In my code I don't show you the information of $serverName, UID, PWD. However in my code I do use them.

I attach my sql server code, my html code and my php code (where is the one in charge of uploading the images). SQL CODE

 CREATE TABLE ImageTable (
     ImageID INT PRIMARY KEY IDENTITY(1,1), 
     ImageData VARBINARY(MAX)
 );

HTML PHP CODE

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Image Upload</title>
</head>
<body>
    <h1>Upload an Image</h1>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        <input type="file" name="image" id="image" accept="image/*" required>
        <input type="submit" value="Upload">
    </form>
</body>
</html>

PHP CODE (UPLOAD)

<?php
$serverName = "";
$connectionOptions = array( 
    "Database"=>"Produccion", 
    "UID"=>"", 
    "PWD"=>"");

// Establish the database connection
$conn = sqlsrv_connect(
    $serverName, $connectionOptions);

if (!$conn) {
    die("Connection failed: " . sqlsrv_errors());
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_FILES["image"])) {
        $img_name= $_FILES['image']['name'];
        $tmp_name= $_FILES['image']['tmp_name'];
        $targetDir = "uploads/"; // Directory where uploaded images will be stored
        $targetFile = $targetDir . basename($img_name);
        
        // Check if the file is an image
        $imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
        if ($imageFileType == "jpg" || $imageFileType == "jpeg" || $imageFileType == "png") {
            move_uploaded_file($tmp_name, $targetFile);
            
            if (move_uploaded_file($tmp_name, $targetFile)) {
                // Read the image data
                $imageData = file_get_contents($targetFile);

                // Insert the image into the database
                $sql = "INSERT INTO ImageTable (ImageData) VALUES ($img_name)";
                $stmt = sqlsrv_prepare($conn, $sql, array(&$imageData, SQLSRV_PARAM_IN, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY), SQLSRV_SQLTYPE_VARBINARY('max')));

                if (sqlsrv_execute($stmt)) {
                    echo "Image inserted into the database successfully.";
                } else {
                    echo "Image insertion failed: ";
                }

                // Delete the uploaded file (optional)
                unlink($targetFile);
            } else {
                echo "Sorry, there was an error uploading your file.";
            }
        } else {
            echo "Only JPG, JPEG, and PNG files are allowed.";
        }
    }
}

// Close the database connection
sqlsrv_close($conn);
?>
0

There are 0 best solutions below