How to Create a Web Barcode Reader App with PHP and Nginx

Dynamsoft Barcode Reader SDK written in C/C++ is capable of reading a variety of 1D and 2D barcode formats including Code 39, Code 93, Code 128, Codabar, QRCode, DataMatrix, PDF417 and so on. In this post, I’m going to demo how to use the SDK to make a simple Web barcode reader app with PHP and Nginx.


Step 1: How to Build PHP Custom Extension with Dynamsoft Barcode Reader SDK

To use Dynamsoft Barcode Reader SDK with PHP, we need to create a PHP custom extension - php_dbr.dll. Read the tutorial Making PHP Barcode Extension with Dynamsoft Barcode SDK  to build your own PHP barcode extension.

Step 2: How to Configure PHP Environment

  1. Copy php_dbr.dll to %php%\ext.
  2. Copy DynamsoftBarcodeReaderx86.dll to PHP root directory %php%.
  3. Open %php%\php.ini and add following line:

Change max file size if you want to upload big image files:


Step 3: How to Configure Nginx for PHP

Open %nginx%\conf\nginx.conf and add PHP configuration:

location ~ \.php$ {
  root           html;
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME <Your Web App Folder>/$fastcgi_script_name;
  include        fastcgi_params;

When uploading big files, you may see the error nginx 413 Request Entity Too Large. To fix it, change file size in configuration file:

client_max_body_size 50M;

Step 4: How to Implement Web Barcode App in PHP on Windows

Create a page index.php.

<!DOCTYPE html>
  <title>Dynamsoft PHP Barcode Reader</title>
  <script src="jquery-1.11.3.min.js"></script>
  <script src="tiff.min.js"></script>
<H1>Dynamsoft PHP Barcode Reader</H1>
<form action="dbr.php" method="post" enctype="multipart/form-data">
    Select barcode image:
    <input type="file" name="readBarcode" id="readBarcode" accept="image/*"><br>
    <input type="submit" value="Read Barcode" name="submit">
<div id="tiff"></div>
<div id='image'></div>
  function reset() {
			var input = document.querySelector('input[type=file]');
			input.onchange = function() {
				var file = input.files[0];
				var fileReader = new FileReader();
    // get file extension
    var extension ='.').pop().toLowerCase();
    var isTiff = false;
    if (extension == "tif" || extension == "tiff") {
      isTiff = true;
				fileReader.onload = function(e) {
      if (isTiff) {
        console.debug("Parsing TIFF image...");
        //initialize with 100MB for large files
          TOTAL_MEMORY: 100000000
        var tiff = new Tiff({
        var tiffCanvas = tiff.toCanvas();
          "max-width": "800px",
          "width": "100%",
          "height": "auto",
          "display": "block",
          "padding-top": "10px"
      else {
        var dataURL =, img = new Image();
        img.src = dataURL;
    if (isTiff) {

Because Dynamsoft Barcode Reader supports TIFF image format, in order to load TIFF in HTML5, we can use the tiff js library.

Create dbr.php for reading uploaded barcode images.

// create absolute file path
function file_build_path(...$segments) {
    return join(DIRECTORY_SEPARATOR, $segments);
$file = basename($_FILES["readBarcode"]["name"]);
echo "<p>$file</p>";
if ($file != NULL && $file != "") {
  // get current working directory
  $root = getcwd();
  // tmp dir for receiving uploaded barcode images
  $tmpDir = "uploads/";
  if (!file_exists($tmpDir)) {
  $target_file = $tmpDir . basename($_FILES["readBarcode"]["name"]);
  $isSuccessful = true;
  $fileType = pathinfo($target_file,PATHINFO_EXTENSION);
  if (!$isSuccessful) {
      echo "Fail to read barcode";
  } else {
      if (move_uploaded_file($_FILES["readBarcode"]["tmp_name"], $target_file)) {
        // dynamsoft barcode reader
        $path = file_build_path($root, $target_file);
         * Description:
         * array DecodeBarcodeFile( string $filename , bool $isNativeOutput [, bool $isLogOn ] )
         * Return Values:
         * If barcode detected, $array[0] is an array.
        $resultArray = DecodeBarcodeFile($path, false);
        if (is_array($resultArray[0])) {
        	$resultCount = count($resultArray);
        	echo "Total count: $resultCount\n";
        	for($i = 0; $i < $resultCount ; $i++) {
        		$result = $resultArray[$i];
                	echo "<p>Barcode format: $result[0], value: $result[1]</p>";
        else {
          echo "<p>$resultArray[0]</p>";
        // delete the uploaded barcode image
      } else {
          echo "Fail to read barcode.";

Run php-cgi:

%php%\php-cgi.exe -b -c %php%\php.ini

Run Nginx:


Visit **localhost:8080/index.php**:

php file upload

php barcode reader

Source Code