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:


// 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

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

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

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
In a world full of worriers, be the warrior.
Leave a comment
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x