Webcam in PHP – How to Use Webcam in PHP

By | August 17, 2012

Webcam in PHP, How to show webcam in PHP, take image from Webcam in PHP read this article you will get all the details regarding these questions. Hello friends i am Vivek Moyal today i am uploading the tutorial for showing the Webcam in PHP page and save that image in database. It will be a nice tutorial for you please comment for the tutorial so we will get motivated. First of all what we need is your PHP IDE, a Webcam, Mysql and a cup of coffee.

PHP IDE is for coding the program, Webcam to take the image, MySql for database and Coffee is for your refreshment…. lol.

In this article we will discuss How to capture the webcam image from our PHP Jquery and save it to the database. It will use the Flash for the webcam screen

In this article we will make 3 Files

  1. Index file for showing the webcam.
  2. Script to upload.
  3. Connection Script for database.

First of all download some files from bottom of the post. Lets start…………..

1. Index File for Webcam . Download link files from here

In this file we will show our Webcam screen on our PHP page. For this download the package from above and link the jquery and swf.


In the above code we show the screen and get the image from webcam. But still we have to save it somewhere so lets go to save this image in our folder.

2. Script for Saving the webcam image

Now after showing the Webcam Screen in PHP page we took the snap from clicking the button. Now we will save it to the folder in our project.

For this we will make a new separate file ….. named …. test.php. But we have to save the snap and call the function for taking the snap and save it and return the success. So first we will make our script to save the image in folder but keep in mind that it will not work because it is just to save it but still we dont have the snap with us.


Let me explain the above code In this code $name will take the current time and data. In $newname we will put our image in images folder along with the image so we provide the full path with the image.

$file will save the image. If there is any error than our if condition will show the error otherwise our script will return the success message.

Now time to take the snap.

add this code in our index.php

Now we have the success message and we will show it in div.

So now we have the code for taking the snap and saving it to our images folder. But still it is not in database now we will update the code for saving the image and saving the name in database.

Create database.

Create database with any name here i am using the “webcam” table name is ‘entry’ with 3 fields.

  1. “id” with auto number
  2. “name” column here i am using it for denoting the person
  3. “image” name.


In the above code we create a connection string with the database. Now lets back to our save image script


we will include the connection file for establishing the connection.

include ‘connection.php’;

now we will write the code for saving the image in our database.

So we have our insert code with us. Now our test.php will look like this.

Now you were thinking that why i am using the session here and why i am taking the id of last insert. It is because in out table we are using the 3 column 1 is id 2nd is for saving the person name 3 is for image name.

from the above code we save the webcam image from php and jquery code in our database but now we want to save the person name on the same image. So we are taking the last id from database with the code and sending the value to the Session

Now we have the id on which our image is saved. In next step we will save the person name in the database on the same id.

Come to the index page here will put some php code and we already have the textfield and button to save the details of the image


Add this above PHP code in our index file so we will save the details of the person with his webcam image in the database.

So in brief………… We make a index file in which we show the Webcam Screen to save the image in our project. By clicking the take snap button it will take snap and by using the test.php we save the image in our folder. Now when we save it our database we just updated the save code and made a connection file. Through getting the last Id and saving it to the session we have the id of the image. In index.php we make the code for saving the extra details of the image by filling the form and submit the form.

In this tutorial we use the JpegCam Library. In this we use following files

PHP Webcam (96.4 KiB, 19652 downloads)
  1. JPEGCam Library
    1. Webcam.js
    2. shutter.mp3
    3. webcam.swf
  2. Test.php
  3. Index.php
  4. Connection.php

Thanks for reading the tutorial how do you find please comment.

PHP Webcam
PHP Webcam
96.4 KiB

310 thoughts on “Webcam in PHP – How to Use Webcam in PHP

  1. Cedrick

    Hey, I just want to know, why is it that the “Take Snapshot” button in index.php isn’t working for me? Thanks for the response!

  2. Brice

    Hi. Thank you for this code, it works very well on my laptop.
    I would like to it on cell phones but I always get this error message: “webcam.js error: could not access webcam. NotSupportrdError: Only secure origins are allowed”. Do you know how to fix this issue? I think by having a SSL certificate (https) I can do it but I am asking myself if there is an easier way.

    1. Vivek Moyal Post author

      Yes if you are running the code over the live server than you have to use HTTPS due to the browser terms and conditions.

  3. amit sharma

    hello vivek,
    I used your code its working fine but when i add one more camera option to take and upload image then its showing error in webcame.js that is
    Uncaught TypeError: this.get_movie(…)._snap is not a function
    please help i stucked in this probelm.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.