PHP Webcam Capture Image Database PHP Script code

Kariuki Maina
By Kariuki Maina 2 Min Read

Here is some sample code that demonstrates how you can use PHP to capture images from a webcam and save them to a database:

<?php

// Connect to the database
$db = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Check if the form has been submitted
if (isset($_POST['submit'])) {
// Check if a file has been uploaded
if (isset($_FILES['image'])) {
// Get the file data
$image = file_get_contents($_FILES['image']['tmp_name']);

- Advertisement -

// Prepare the SQL statement
$stmt = $db->prepare("INSERT INTO images (image) VALUES (:image)");

// Bind the parameter
$stmt->bindParam(':image', $image, PDO::PARAM_LOB);

// Execute the statement
$stmt->execute();

// Redirect to the list of images
header('Location: list.php');
exit;
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Webcam Capture</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" accept="image/*" capture="camera" name="image">
<input type="submit" name="submit" value="Save">
</form>
</body>
</html>

This code assumes that you have a database with a table named “images” that has a column named “image” for storing the captured images. It also assumes that you are using the PDO extension for accessing the database.

The form in the HTML code uses the accept and capture attributes to allow the user to select an image file from the device’s camera. When the form is submitted, the code gets the file data from the $_FILES array and inserts it into the database using a prepared SQL statement.

I hope this helps! Let me know if you have any questions.

Share this Article
Kariuki Maina
By Kariuki Maina Kariuki Maina
Follow:
In a world full of worriers, be the warrior.
Leave a comment
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
adbanner