1

I've been trying for a few weeks now to fire a batch file that runs the command to scan a document from a scanner attached to the same machine.

I'm working on a web app that (using PHP) is meant to gather information from a patron who is trying to apply for a membership at my establishment. I'm at a loss for how to explain what I've tried because I think I've tried just about every combination of exec(), shell_exec() and system() I can think of.

I have PHP permissions set to execute, and (I think) all of the permissions on any other files set correctly but I'm still getting nowhere.

Any pointers and/or assistance is greatly appreciated as this is the only piece of this app that is keeping me from finishing.

References: I'm using an open source piece of software to do the actual scanning called wia-cmd-scanner. But to note, I have also tried NAPS2 with the same lack of results The very small site is hosted in IIS. Scan.bat is a simple one liner.

c:\inetpub\websvcs\selfserve\kiosk\bin\wia-cmd-scanner.exe /w 94.75 /h 53.975 /dpi 600 /color RGB /format JPG /output .\id.jpg

Various funcions in PHP I have tried are:

Executing via batch:

<?php
$batchFile = "C:\\inetpub\\websvcs\\selfserve\\kiosk\\bin\\scan.bat"
exec("cmd /c \"{$batchFile}\"");
?>

Executing directly:

$command = "bin\\wia-cmd-scanner.exe";
$args = " /w 94.75 /h 53.975 /dpi 600 /color RGB /format JPG /output .\id.jpg";
$fire = $command . $args;
exec("cmd /c {$fire} 2>&1");

Same with shell_exec():

$command = "bin\\wia-cmd-scanner.exe";
$args = " /w 94.75 /h 53.975 /dpi 600 /color RGB /format JPG /output .\id.jpg";
$fire = $command . $args;
shell_exec($fire);

These are just 3 examples but I feel like I've tried 30 at least. In many cases I got no error and nothing logged but nothing scanned either. In some cases It would give me Return Value:1, but nothing more. And the last function I tried.

<?php
$batchFile = "C:\\inetpub\\websvcs\\selfserve\\kiosk\\bin\\scan.bat";
echo system("cmd /c \"{$batchFile}\"");
?>

Gave the error

"Exception occured: Cannot create ActiveX component."

I feel like I may have been getting close but I just can't figure out what is keeping this thing from firing. My guess is it is something to do with escaping, but I'm not sure where. Admittedly I am fairly new to programming in PHP.

Any help is appreciated.

Edit: Using this code saved as new39.php...

<?php
$command = "c:\\inetpub\\websvcs\\selfserve\\kiosk\\bin\\wia-cmd-scanner.exe";
$args = " /w 94.75 /h 53.975 /dpi 600 /color RGB /format JPG /output c:\\inetpub\\websvcs\\selfserve\\kiosk\\temp\\id.jpg";
$fire = $command . $args;
$output = [];
$return_var = 0;
exec($fire);
echo "Return status: " . $return_var . "\n";
?>

I can run execute from the command line (eg: c:\PHP c:\inetpub\websvcs\selfserve\kiosk\new.39.php) and the code runs as it should. However when I open a browser and load the same file I get Return Status: 0. The scanner does not activate or of course save anything.

11
  • [first thing to do]: change bin\\wia-cmd-scanner.exe to FULL PATH (e.g. c:\xxxx\xxxx\xxxx\bin\wia-cmd-scanner.exe) and try again. (do not use batch file) Commented Oct 14 at 11:16
  • Using this code.... <?php $command = "c:\\inetpub\\websvcs\\selfserve\\kiosk\\bin\\wia-cmd-scanner.exe /w 94.75 /h 53.975 /dpi 600 /color RGB /format JPG /output .\id.jpg"; $output = []; $return_var = 0; exec($command, $output, $return_var); echo "Output:\n"; foreach ($output as $line) { echo $line . "\n"; } echo "Return status: " . $return_var . "\n"; ?> I get the error... Output: Exception occured: Cannot create ActiveX component. Return status: 0 Commented Oct 14 at 12:14
  • It's worth noting that if I remove any arguments from the command it executes as it should. (same as executing wia-cmd-scanner.exe /?). So it's in the way It's receiving the arguments I'm assuming. Commented Oct 14 at 12:25
  • 1
    I've run it from the command line as "c:\\inetpub\\websvcs\\selfserve\\kiosk\\bin\\wia-cmd-scanner.exe /w 94.75 /h 53.975 /dpi 600 /color RGB /format JPG /output .\id.jpg" and yes, that does activate the scanner and save the file. I can also run that via PHP from the command line eg: "c:\PHP c:\inetpub\websvcs\selfserve\kiosk\new39.php" and that TOO activates the scanner and saves the file. However, when I open a browser and surf to "localhost/new39.php" I get only "Return Status: 0". Scanner does not activate. Commented Oct 14 at 13:10
  • 1
    You might find that the PHP user in the terminal is different from the user on the web server. Or you might find that the relative paths are different. Or, possibly certain functions are disabled in the PHP ini. (e.g. disable_functions=exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source, check for stuff like that) Commented Oct 14 at 13:26

1 Answer 1

-1

PHP exec has the following parameters:

  • $command: the command you wish to execute
  • &$output: the output you receive. This is passed by reference, so you might need an $output variable or something of the like for this to work
  • &$result_code: the result code, also passed by reference

So you can test via

exec("cmd /c \"{$batchFile}\"", $output, $result_code);

Check what $output has at the end as well as $result_code. Also, try running it directly and debug if needed until it works. Then proceed with debugging the call for exec until it works. As about the ActiveX component creation issue you may stumble into, look into this question: Getting error "Cannot create Active X component" from Windows service but not console Application

Sign up to request clarification or add additional context in comments.

1 Comment

If this answer is unhelpful, then I would appreciate to see the reason for not being helpful, so I could make amends

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.