How to Scan Documents in PHP Applications
How to Interact with TWAIN Scanners in a PHP Application
PHP is a popular server-side language, but it cannot directly interact with document scanners attached to client computers. However, with the continued growth of digitization, scanning capabilities have become increasingly important in PHP environments.
To implement document scanning in your PHP web applications, you can use an HTML5/JavaScript scanning SDK like Dynamic Web TWAIN to interact with TWAIN, SANE, eSCL, WIA, and other scanners attached to the client machine.
Image Scanning and Uploading in PHP Application
Here are the code snippets for using Dynamic Web TWAIN to scan documents and upload the images to Web server in PHP.
JavaScript code – scan and upload images
function AcquireImage(){
var DWObject = Dynamsoft.DWT.GetWebTwain('dwtcontrolContainer');
DWObject.IfShowUI = false;
DWObject.SelectSource();
DWObject.OpenSource();
DWObject.AcquireImage();
}
function Upload() {
if (DWTObject && DWTObject.HowManyImagesInBuffer > 0) {
//Path to the server-side script. You can adjust it according to the actual path of the upload server you deployed.
var strUrl = "http://{server-path}/Sample/Upload/SaveToFile.php";
var imgAry = [DWTObject.CurrentImageIndexInBuffer];
DWTObject.HTTPUpload(
strUrl,
imgAry,
Dynamsoft.DWT.EnumDWT_ImageType.IT_PNG,
Dynamsoft.DWT.EnumDWT_UploadDataFormat.Binary,
"WebTWAINImage.png",
onUploadSuccess,
onUploadFailure);
} else {
alert("There is no image in buffer.");
}
}
function onUploadSuccess() {
alert("Upload successful");
}
function onUploadFailure(errorCode, errorString, sHttpResponse) {
alert(sHttpResponse.length > 0 ? sHttpResponse : errorString);
}
PHP code – action page for uploading and receiving the images to web server
<?php
$tmpFile = $_FILES['RemoteFile']['tmp_name'];
$fileSize = $_FILES['RemoteFile']['size'];
$rawFileName = $_FILES['RemoteFile']['name'];
$folderName = time();
// create folder
if(!is_dir("UploadedImages")) {
mkdir("UploadedImages");
}
$newFolder = "UploadedImages" . DIRECTORY_SEPARATOR . $folderName;
if(!is_dir($newFolder)) {
mkdir($newFolder);
}
// get info
$_fields = "info:";
$count = count($_POST);
if ($count > 0) {
foreach ($_POST as $key => $value) {
if($key == "CustomInfo") {
$_fields = "info:" . $value;
break;
}
}
}
// save file
$fileFullName = $newFolder . DIRECTORY_SEPARATOR . $rawFileName;
if (file_exists($fileFullName)) {
$fWriteHandle = fopen($fileFullName, 'w');
} else {
$fWriteHandle = fopen($fileFullName, 'w');
}
$fReadTmpFileHandle = fopen($tmpFile, 'rb');
$fileContent = fread($fReadTmpFileHandle, $fileSize);
fclose($fReadTmpFileHandle);
fwrite($fWriteHandle, $fileContent);
fclose($fWriteHandle);
echo $_fields . "|filename:" . $rawFileName . "|folder:" . $folderName;
?>
Regarding Dynamic Web TWAIN
Dynamic Web TWAIN is a browser-based document scanning component that can interact with TWAIN & SANE scanners from all popular web browsers. It is highly optimized for 32-bit/64-bit IE, Edge, Firefox, Chrome, Safari, and Opera on Windows, Linux, and macOS.
Regarding server-side technology, Dynamic Web TWAIN works with all programming languages, including C#, VB.NET, ASP, JSP, and PHP. It can be deployed on web servers like IIS, Apache, or Tomcat.
The server-side code can be written in your preferred language. The “Action Page” refers to the page that receives and processes the images uploaded by Dynamic Web TWAIN. These images can be saved into a file system, database, SharePoint, or other formats.
To embed a web scanning module into your PHP application, you can download and install the 30-day free trial of Dynamic Web TWAIN.
If you have any questions or comments about writing a PHP application to interact with scanners, please let us know.
Check the guide on How to Deploy an Upload Server Using PHP
Explore Our Developer Hub for Guides, API References, and More.