如何在PHP中扫描

时间:2014-12-11 23:35:51

标签: php linux scanning

我想从我的PHP脚本中扫描一下。我正在使用Ubuntu 14.04 LTS,Brother MFC-7840W扫描仪(位于工作中)和Brother MFC-9840CDW(位于家中)。当扫描仪作为网络扫描仪连接到计算机时,我可以从终端和PHP进行扫描。但是,当扫描仪作为USB扫描仪连接到计算机时,我无法从PHP扫描(我仍然可以从终端扫描)。

为什么我的PHP脚本无法访问USB扫描仪,但$ USER能够?

我在工作中启动了这个问题,但现在我在家,所以我将尝试从我的PHP脚本访问Brother MFC-9840CDW USB扫描仪。

以下是我用来扫描的PHP代码片段:

if($_POST['ScanDevice'] == "brother3:net1;dev0") // if MFC-7840W network scanner
{$scanner = escapeshellarg($_POST['ScanDevice']);} 
elseif($_POST['ScanDevice'] == "brother3:bus3;dev1") // if MFC-7840W USB scanner
{$scanner = escapeshellarg($_POST['ScanDevice']);}
elseif($_POST['ScanDevice'] == "brother3:net1;dev1") // if MFC-9840CDW network scanner
{$scanner = escapeshellarg($_POST['ScanDevice']);}
elseif($_POST['ScanDevice'] == "brother3:bus6;dev1") // if MFC-9840CDW USB scanner
{$scanner = escapeshellarg($_POST['ScanDevice']);}

$command = "scanimage -d {$scanner} --resolution {$_POST[ScanResolution]} --mode {$_POST[ScanColor]}  > {$filename}";

echo exec($command,$op,$result);
if($result > 0)
{die("ERROR");}

PHP脚本适用于网络扫描程序,但不适用于USB扫描程序 如果我选择任一USB扫描仪(当前为MFC-9840CDW)并运行脚本,则文件/var/log/apache2/error.log显示:

scanimage: open of device brother3:bus6;dev1 failed: Invalid argument  

问题出现了:设备brother3:bus6;dev1是否存在?

以下是{i}在家中的终端输入时显示的内容(MFC-9840CDW所在的位置):

scanimage --list-devices

为了证明USB扫描器适用于$ USER,我在终端输入以下命令:

[pixma] udp_command: No data received (select): timed out
[pixma] udp_command: No data received (select): timed out
[pixma] udp_command: No data received (select): timed out
[pixma] Cannot read scanner make & model: �+�&
device `brother3:net1;dev1' is a Brother MFC-7840W SCANNER
device `brother3:net1;dev0' is a Brother MFC-9840CDW Scanner-MFC-9840CDW
device `brother3:bus6;dev1' is a Brother MFC-9840CDW USB scanner

显示:

scanimage --test -d 'brother3:bus6;dev1'  

为了证明用户www-data无法访问USB扫描仪,我在终端输入以下命令:

scanimage: rounded value of br-x from 215.9 to 215.88
scanimage: rounded value of br-y from 355.6 to 355.567
scanimage: scanning image of size 1664x2776 pixels at 24 bits/pixel
scanimage: acquiring RGB frame, 8 bits/sample
scanimage: reading one scanline, 4992 bytes...  PASS
scanimage: reading one byte...      PASS
scanimage: stepped read, 2 bytes...     PASS
scanimage: stepped read, 4 bytes...     PASS  

显示:

sudo -u www-data scanimage --test -d 'brother3:bus6;dev1'  

1 个答案:

答案 0 :(得分:3)

PHP无法访问USB扫描程序,因为www-data(运行PHP脚本的用户)不是USB扫描程序所属组的成员。将用户www-data添加到USB扫描仪所属的组中。

要查找USB扫描仪所属的组,必须知道USB扫描仪的名称。要查找USB扫描仪的名称,请输入命令:

lsusb -v  

显示(以及其他行):

Bus 002 Device 007: ID 04f9:01cc Brother Industries, Ltd  

然后,输入命令:

,找到USB扫描仪所属的组
ls -al /dev/bus/usb/002/007  

显示:

crw-rw-r--+ 1 root lp 189, 134 Dec 12 22:30 /dev/bus/usb/002/007  

USB扫描仪所属的组是 lp 。 $ USER能够访问USB扫描仪的原因是因为$ USER是 lp 组的成员,而www-data不是。输入命令即可证明:

grep ^lp /etc/group  

显示:

lp:x:7:root,arya  

通过输入以下命令,将用户 www-data 添加到 lp 组:

sudo usermod -a -G lp www-data  

然后,再次测试结果,输入grep ^lp /etc/group,现在显示:

lp:x:7:root,arya,www-data  

然后,重新启动apache以确保上面的操作已注册:

sudo apache2ctl -k restart  

然后,测试一下www-data是否可以从终端访问USB扫描仪:

sudo -u www-data scanimage --test -d 'brother3:bus6;dev1'  

显示:

scanimage: rounded value of br-x from 215.9 to 215.88  
scanimage: rounded value of br-y from 355.6 to 355.567  
scanimage: scanning image of size 1664x2776 pixels at 24 bits/pixel  
scanimage: acquiring RGB frame, 8 bits/sample  
scanimage: reading one scanline, 4992 bytes...  PASS  
scanimage: reading one byte...      PASS  
scanimage: stepped read, 2 bytes...     PASS  
scanimage: stepped read, 4 bytes...     PASS  

然后,再次运行原始PHP脚本以查看它是否可以访问USB扫描仪并扫描文档......

成功!