system_in php,exec() and system() in PHP

前提:

将SWFTools安装到目录: C:\Vin\SWFTools\。然后在系统环境变量中,给path增加此路径。我们在路径C:\temp\下放置一个测试用的PDF样品:testSample.pdf。

在命令提示器中执行pdf2swf,将pdf输出成swf,测试成功:

0818b9ca8b590ca3270a3433284dd417.png

环境:Windows 7 + XAMPP 1.7.3(PHP 5.3.1, Apache 2.2.14, MySQL 5.1.41)

因为之前发现的一个bug,所以在XAMPP中并未将Apache注册为服务。而在这种状态下,不可能为Apache指定登陆账号。

测试exec函数,做如下PHP文件:

$o = array();

$a = exec('cmd.exe', $o, $s);

var_dump($o);

var_dump($s);

var_dump($a);

?>

在浏览器中执行此PHP文件,得到的输出是:

array(4) {

[0]=>

string(33) "Microsoft Windows [版本 6.1.7601]"

[1]=>

string(63) "Copyright (c) 2009 Microsoft Corporation. All rights reserved."

[2]=>

string(0) ""

[3]=>

string(25) "C:\xampp\htdocs\lab\exec>"

}

int(0)

string(25) "C:\xampp\htdocs\lab\exec>"

首先PHP本身执行成功。按照官网的文档介绍来看,cmd.exe的返回值是0(即$s的值),这说明cmd.exe执行成功,PHP的exec()函数返回的字符是cmd.exe最后的状态。数组$o则保存了执行过程中cmd.exe打印到屏幕上的所有文字。

实验一:

实验前先删除掉前面创建的swf文件。创建如下PHP文件:

$o = array();

$a = exec('pdf2swf.exe C:\temp\testSample.pdf -o C:\temp\testSample.swf', $o, $s);

var_dump($o);

var_dump($s);

var_dump($a);

?>

在浏览器中执行这个PHP,得到的传回是:

array(0) {

}

int(1)

string(0) ""

并且C:temp\下也没有swf生成。

根据Windows System Error Codes (exit codes)来看,返回值是1,表示遇到了不正确的函数。即是说pdf2swf.exe被认为是不正确的函数。我回到exec的官方在线文档,有一个叫Martin Lakes的人留下过一个评论,其中讨论到在执行的命令行后面加“2>&1”:

There are a few thing that are important here.

First of all: put the full path to the php binary, because this command will run under the apache user, and you will probably not have command alias like php set in that user.

Seccond: Note 2 things at the end of the command string: the '2>&1' and the '&'. The '2>&1' is for redirecting errors to the standard IO. And the most important thing is the '&' at the end of the command string, which tells the terminal not to wait for a response.

那么把我的命令行字符改成:pdf2swf.exe C:\temp\testSample.pdf -o C:\temp\testSample.swf 2>&1,再重新执行,得到:

array(1) {

[0]=>

string(56) "'pdf2swf.exe' 不是內部或外部命令、可執行的程式或批次檔。"

}

int(1)

string(56) "'pdf2swf.exe' 不是內部或外部命令、可執行的程式或批次檔。"

按照上面那段话的提示,PHP默认情况下是在Apache用户下执行。这到底表示什么?从实验前面对cmd.exe的实验可以看出执行的上下文正是PHP文件所在的路径,即C:\xampphtdocs\lab\exec\,那么我们在这个路径下执行下命令:pdf2swf.exe C:\temp\testSample.pdf -o C:\temp\testSample.swf,结果是成功:

0818b9ca8b590ca3270a3433284dd417.png

现在,唯一可以尝试的就是在pdf2swf.exe前加上它的完整路径名,再试一次执行下面的PHP:

$o = array();

$a = exec('C:\Vin\SWFTools\pdf2swf.exe C:\temp\testSample.pdf -o C:\temp\testSample.swf 2>&1', $o, $s);

var_dump($o);

var_dump($s);

var_dump($a);

?>

收到的结果是:

array(3) {

[0]=>

string(43) "NOTICE processing PDF page 1 (612x792:0:0)"

[1]=>

string(35) "NOTICE File contains jpeg pictures"

[2]=>

string(47) "NOTICE Writing SWF file C:\temp\testSample.swf"

}

int(0)

string(47) "NOTICE Writing SWF file C:\temp\testSample.swf"

检查C:\temp\路径,有一个新创建的swf文件。哦耶!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值