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

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

  1. ibrahim

    Hi Vivek,
    Me waiting for your reply….! i need it urgently..! take_snapshot() and webcam.configure() functions

  2. Ibrahim

    i wann write class base coding in php. If you know can you guide me. It’s help to reduce the code.
    Reply as soon as possible to my Id or in this blog only.

    Thank You.

  3. Ibrahim

    Thanks for the tutorial .
    I click on Take Snapshot button when i click it nothing happens & searching from where u have called take_snapshot() and webcam.configure() functions and i searched both these functions but couldn’t find it so can u send me the code to my Id,
    Thank U

  4. Baburao

    Hello, Vivek
    I downloaded the code , and it works fine untill I click on Take Snapshot button when i click it nothing happens & searching from where u have called take_snapshot() and webcam.configure() functions and i searched both these functions but couldn’t find it so can u send me the code to my id—

    I need it’s urgent , I hope u send me the code …… Thank U

  5. Aashu

    Dear Sir,

    FInally i have done it in php-4

    1. Vivek Moyal Post author

      Sorry for my late reply as i was on leave ….. thats great that you have done this at your end and you put the whole code here also….. it will help the other’s too.. thank you for your help

  6. Aashu

    Dear Sir,

    I think it does not support in php 4–$file = file_put_contents( $newname, file_get_contents(‘php://input’) );, file_put_contents so what should i use or if anything please let me know.

  7. Aashu

    Dear Sir,

    The set of instructions which you have posted i run it on php-5 using xamp is working gracefully but when i run it on php-4 it’s throwing an
    PHP Error: unknown And Div Section Has uploading… And Image Just Stucked up.
    Please Let Me Know The Solution I Have To Run This Program Using PHP-4 …

  8. Aashu

    Dear Sir,

    How May I Run This Code In Php Version 4 .
    If I am running in Php 5 it’s working superbly.
    but when i run in php 4 –
    I get an alert : php error unknown and uploading div and image just hold.
    thereafter nothing is happening.
    Please Sir:

  9. Aashu

    Dear Sir,

    I have installed xamp-which has (mysql-SELECT VERSION();–5.6.20) and (PHP Version 5.5.15).
    Now I have downloaded the code and paste it inside the htdocs and extract it.thereafter i check the connection.php it’s exactly the same configuration which i am running then i did not edit anything.
    thereafter i created a database -webcam and then table inside webcam database-
    CREATE TABLE `entry` (
    `id` bigint(10) NOT NULL AUTO_INCREMENT,
    `images` varchar(200) DEFAULT NULL,
    `name` varchar(200) DEFAULT NULL,
    PRIMARY KEY (`id`)
    Next i have mozilla firefox version-25.0.1.
    I opened and put the -http://localhost/camTest/
    Then i clicked on allow of 1st camera screen now my lapy web cam on and snap is seen.
    and clicked on take sanpshot -nothing happened.
    then i write simple upload script that is uploaded the image easily.
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);

    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] 0) {
    echo “Return Code: ” . $_FILES["file"]["error"] . “”;
    } else {
    echo “Upload: ” . $_FILES["file"]["name"] . “”;
    echo “Type: ” . $_FILES["file"]["type"] . “”;
    echo “Size: ” . ($_FILES["file"]["size"] / 1024) . ” kB”;
    echo “Temp file: ” . $_FILES["file"]["tmp_name"] . “”;
    if (file_exists(“upload/” . $_FILES["file"]["name"])) {
    echo $_FILES["file"]["name"] . ” already exists. “;
    } else {
    “images/” . $_FILES["file"]["name"]);
    echo “Stored in: ” . “images/” . $_FILES["file"]["name"];
    } else {
    echo “Invalid file”;


    then i checked the webconsole in js there is showing an error–
    ReferenceError: take_snapshot is not defined

    Sir Please Let Me Know The Proper Solution.!!

  10. Deal seeking mom

    Heya i’m for the first time here. I found this board and I find
    It really useful & it helped me out a lot. I hope to give something back and help others like you aided me.

  11. praveen kumar

    ThaNks For your Script Bro :) Its so nice and much more helpfull myself

  12. RJ Chaudhary

    Dear Vivek Moyal Sir.
    I found best code for php webcam application from here and I downloaded all file and create “webcam” folder in “localweb” folder and make “images” folder in “webcam” folder after that I create database in mysql named “webcam” and table named “entry” and field id int(11) auto_increment, name varchar(100), image varchar(100)
    all instruction I followed in this page but after-all error occurred as bellow so please help me.
    When I type name in text box and click on submit button then below error showed me..
    Notice: Undefined index: myvalue in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\webcam\index.php on line 7

    Notice: Undefined index: myvalue in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\webcam\index.php on line 11
    Updated re …..

    So then I Click on Take Snapshot there are no capture voice and not image saved in images folder

    after I chacked, so there are also error ERROR: Failed to write data to 0, check permissions
    So please help, Thanking you.

    1. Vivek Moyal Post author

      If you are getting the write error than please check the permission of the folder. It should be write enabled.

      Here myvalue is a session value and what you are getting is Notice. so dont consider it. Just turn off the notices it will go away

      1. RJ Chaudhary

        Thank you for quick reply.
        I checked user permission and grant full access to everyone user.
        I used ATTRIB command ATTRIB -r -h -r /S C:\…..\lacalweb\webcam
        but same problem exist…. as my above post…
        So please help me how can access folder permission by appachi server on windows 7
        Thank you

  13. Jaspreet

    Hi Vivek

    Firstly, thanks for writing up such a nice script.

    I am facing up a lil problem in chrome. I guess it something with flash-player settings or some other thing.
    Its giving me this error in console.
    Uncaught Error: Error calling method on NPObject. on this line of webcam.js
    this.get_movie()._snap( this.api_url, this.quality, this.shutter_sound ? 1 : 0, this.stealth ? 1 : 0 );

    else on IE and firefox its working like a fly. If you can tell me about this than i will be grateful.
    And one more thing if i want to send some extra parameter on the php side(test.php) how can i do that without using session means sending the post request with the uri ??



Leave a Reply

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