Monitor Folder With PHP

Published: 2009-10-16

At the office we use a RICOH Aficio 1045 Copier. One of the features of this copier is that it also doubles as a scanner. To scan a document an employee goes to the copier, puts the copier in scanner mode, selects their mailbox from a list and then scans the document. The scanner then sends the scanned information to the scanner software running on one of our servers where the actual file is created and placed in the user's virtual mailbox. (We use pdf as the default.)

Each user's virtual mailbox has a folder associated with it, and this folder is located in each user's personal folder on the server. Most of our employees have workstations and their "My Documents" folder is mapped to their personal folder on the server. By the time they get back to their desk, all they have to do is go into their "My Documents" folder and the scanned document is already there.

The problem is with notebook users who don't have a mapped "My Documents". If it were mapped, they would not be able to take their documents with them whenever they left the office. So, how can notebook users also retrieve their scanned documents from their "My Documents" folder?

Basically, all that is needed is some kind of mechanism to monitor a user's virtual mailbox, and when a new file is placed there, copy it down to the user's "My Documents" folder on the notebook.

PHP Solution:

Install PHP on your server/workstation that will be used to run this. (Google "PHP Install" for more info...) Php doesn't have a built in way of creating Windows services, hence the "almost" in the title. So you can use two nice utilities called INSTSRV.EXE and SRVANY.EXE that come with the Windows Resource Kit.

You can download it from here: Windows 2003 Resource Kit Tools

Once you've installed the kit you can use something similar to the following to install the service.

C:\Program Files\Windows Resource Kits\Tools>instsrv scansmon "d:\Program Files\Windows Resource Kits\Tools\srvany.exe"

SRVANY does all the work and Windows sees it as the service. SRVANY will look in the registry for the EXE file to use for the service. The EXE name, the starting directory, and any optional parameters should be in the registry as follows:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\
Services\scansmon\Parameters]
"AppDirectory"="D:\\Scripts\\Scansmon"
"Application"="php -q D:\\Scripts\\Scansmon\\scansmon.php3"
"AppParameters"=""

NOTE: You can copy/paste the lines above to a file named scansmon.reg (change the paths as necessary and make sure to include the double back-slashes) and double click the .reg file to add these entries to the registry. Php itself is the EXE that it runs. scansmon.php3 has the following code in it. (you will need to modify it for your network)

<?php
set_time_limit(0);

$user_a_user_directory = '\\\\Server01\\user\\user_a\\ScansIn\\';
$user_a_notebook_scans_directory = '\\\\user_a_notebook\\c$\\Documents and Settings\\user_a\\My Documents\\Scans\\';

$user_b_user_directory = '\\\\Server01\\user\\user_b\\ScansIn\\';
$user_b_notebook_scans_directory = '\\\\user_b_notebook\\c$\\Documents and Settings\\user_a\\My Documents\\Scans\\';

$loop = true;

while($loop){ //Will run as a windows service so loop infinitely
    $log_file = date("Ymd").".txt";

    writetolog("****************", $log_file);
    try{
        if (file_exists($user_a_user_directory) && file_exists($user_a_notebook_scans_directory)){
            writetolog("Ready to proceed with user_a scans...", $log_file);

            $oldfile = "";
            $newfile = "";

            if ($dir = opendir($user_a_user_directory)){
                while ($file = readdir($dir)){ //As long as there are files copy them and write to log
                    $oldfile = $user_a_user_directory.$file;
                    if (is_file($oldfile)){ //&& substr($file, -4) == '.pdf'
                        $newfile = $user_a_notebook_scans_directory.$file;
                        writetolog("copying $file", $log_file);
                        if (!copy($oldfile, $newfile)){
                            writetolog("failed to copy $oldfile...", $log_file);
                        }else{
                            unlink($oldfile);
                            writetolog("deleted $oldfile...", $log_file);
                        }
                    }
                }
                closedir($dir);
            } 
        }else{
            writetolog("Can't do user_a scans...", $log_file);
        }

        if (file_exists($user_b_user_directory) && file_exists($user_b_notebook_scans_directory)){
            writetolog("Ready to proceed we user_b scans...", $log_file);

            $oldfile = "";
            $newfile = "";

            if ($dir = opendir($user_b_user_directory)){
                while ($file = readdir($dir)){
                    $oldfile = $user_b_user_directory.$file;
                    if (is_file($oldfile)){ //&& substr($file, -4) == '.pdf'
                        $newfile = $user_b_notebook_scans_directory.$file;
                        writetolog("copying $file", $log_file);
                        if (!copy($oldfile, $newfile)){
                            writetolog("failed to copy $oldfile...", $log_file);
                        }else{
                            unlink($oldfile);
                            writetolog("deleted $oldfile...", $log_file);
                        }
                    }
                }
                closedir($dir);
            } 
        }else{
            writetolog("Can't do user_b scans...", $log_file);
        }

        writetolog("Waiting for 180 seconds...", $log_file);
        Sleep(180); //Wait three minutes before next check
    }
    catch(Exception $e){
        $loop = false;
        writetolog($e->getMessage(), $log_file);
    }
}

function writetolog($text, $file)
{
    $log_directory = 'D:\\Logs\\Scripts\\';
    $log_file = $log_directory.$file;
    $fh = fopen($log_file, 'a');
    fwrite($fh, date("h:i:s A")."  $text".chr(13).chr(10));
    fclose($fh);
}
?>