Webcam in PHP – How to Use Webcam in PHP

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, 19525 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


  1. hello sir ,that’s nice titorial , i have successfully run this in chrome but this is not work in firefox. please help me.

    • I am writing a new article over this with new files so wait for 1 or 2 day you will find the new article about the webcam. It will work with firefox also

      • amit sharma

        July 7, 2017 at 14:46

        can i use same code on same page for multiple camera, because i was trying but it was not working. please help me out to solve this issue.

  2. It shows movie not loaded yet

  3. i use your code its working fine in htdocs but not working in may project folder. Please help.

  4. Thank you for nice tutorial.But plugin is not support on browser in ubuntu.

  5. Hi, thanks for the very useful tutorial, im getting an error, like the Linda error, when i click take snapn shot nothing seems to happen, and when i click submit i get this error: Undefined index: myvalue in C:\wamp\www\webcam\index.php on line 90, thanks in advance.

  6. sir , snapshot is not working and when i press submit it shows uploaded successfully, but i see nothing in database and image folder.

  7. Parse error:Syntax error,unexpected “”, expecting identifier (T_STRING) or variable (T_VARIABLE)or number (T_NUM_STRING) in C:\xampp\htdocs\…\index.Php on line 24
    Please help sir,all the l appreciate your efforts to enrich us.

  8. Column name is “images” and not “image”

  9. I can’t take snapshot and save to database, when i click snapshot, camera not taking the picture and do nothing. when i click “submit query” there are error like this

    Notice: Undefined index: myvalue in C:\xampp\htdocs\camTest\index.php on line 7

    Notice: Undefined index: myvalue in C:\xampp\htdocs\camTest\index.php on line 13

  10. Hi sir ………. can you tell me can we record also video using this plugin ……..
    i saw i scriptcam but it to complicated ….

  11. ReferenceError: take_snapshot is not defined
    Please help me out to solve this error.

  12. Thank you for nice tutorial

  13. Snapshot not run and I can’t upload image into database…. Please, Let me kno why…

    Webcam.snap( function(data_uri) {
    // display results in page
    document.getElementById(‘results’).innerHTML =
    ‘Here is your image:’ +
    } );

Leave a Reply

Your email address will not be published.