php exec:不返回输出【JAVA教程】

!
也想出现在这里? 联系我们
信息

php exec:不返回输出,第1张

概述php exec:不返回输出

我有这个问题:在ISS Web服务器上,安装了windows 7 x64专业版,Zend服务器。 在PHP下运行这个命令:

exec(\’dir\’,$output,$err);

$output is empty,$err=1. 所以exec没有回应输出,似乎有一些错误。 PHP disable_functions是空的,PHP不安全模式,是标准的,我检查所有的选项。 这似乎是一个普遍的错误,即使search谷歌不给结果。

请写每一个他的经验和最终的解决scheme或解决方法。

用PHP运行一个外部程序

在windows XP中,是否有可能获得由exec()启动的进程的PID?

如何在find命令的linux中的{}参数上应用正则Expression式

从PHP运行linux命令

执行linux命令的PHP过早终止?

为什么不执行(“顶”); 在linux上工作?

从python运行代码具有不同于bash的权限?

通过exec()在windows 8.1中执行具有pipe理员权限的批处理脚本

PHP exec用IIS更改windows密码

程序执行的过程以及如何处理虚拟地址

PHP手册的相关部分有几个帖子,比如这个 :

我无法使用PHP exec命令来执行任何批处理文件。 执行其他命令(即“dir”)工作正常)。 但是,如果我执行了一个批处理文件,我从exec命令没有收到任何输出。

我有的服务器设置包括运行IIS6和PHP 5.2.3的windows server 2003服务器。 在这个服务器上,我有:

授予Internet用户对c: windows system32 cmd.exe的执行权限。

授予Everyone->完全控制到写入批处理文件的目录。

授予Everyone->完全控制整个c: cygwin bin目录及其内容。

授予Internet用户“以批处理身份登录”权限。

指定每个正在执行的文件的完整路径。

测试这些脚本从服务器上的命令行运行,他们工作得很好。

确保%systemroot% system32位于系统路径中。

事实证明,即使在服务器上的所有上述位置,我也必须在exec调用中指定cmd.exe的完整路径。

当我使用这个调用时: $output = exec(\”c:\\windows\\system32\\cmd.exe /c $batchfileToRun\”);

那么一切正常。 在我的情况下, $batchfileToRun是批处理文件的实际系统路径(即调用realpath()的结果)。

exec和shell_exec手册页上还有几个。 也许跟随他们将得到它为你工作。

从命令(如dir )获得零输出的主要原因是dir不存在。 它是命令提示符的一部分,对于这个特定问题的解决方案是很好的,在这个页面的某个地方。

你可以通过按下WIN + R ,然后键入dir或start查找,就会出现一个错误信息!

然而,这听起来可能是错误的,我发现在windows中执行与进程相关的最可靠的方法是使用组件对象模型。 你说我们分享我们的经验吧?

我听到人们在笑

恢复了你的镇静呢?

所以,首先我们将创建COM对象:

$pCOM = new COM(\”WScript.Shell\”);

然后,我们将运行需要运行的任何东西。

$pShell = $pCom->Exec(\”C:Random FolderWhatever.exe\”);

凉! 现在,这将挂起,直到一切完成在二进制文件。 所以,我们现在需要做的是抓住输出。

$sstdOut = $pShell->StdOut->ReadAll; # Standard output $sstdErr = $pShell->StdErr->ReadAll; # Error

还有一些其他的事情你可以做 – 找出什么是进程ID,错误代码等。虽然这个例子是针对Visual Basic的,但它是基于相同的方案。

编辑:经过几个研究,我看到执行DIR命令的唯一方法是这样的:

cmd.exe /c dir c:

所以你可以试试我的解决方案:

$command = \’cmd.exe /c dir c:\\\’;

你也可以使用proc_open函数,它可以访问stderr,返回状态码和stdout。

$stdout = $stderr = $status = null; $descriptorspec = array( 1 => array(\’pipe\’,\’w\’),// stdout is a pipe that the child will write to 2 => array(\’pipe\’,\’w\’) // stderr is a pipe that the child will write to ); $process = proc_open($command,$descriptorspec,$pipes); if (is_resource($process)) { // $pipes Now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // 2 => readable handle connected to child stderr $stdout = stream_get_contents($pipes[1]); fclose($pipes[1]); $stderr = stream_get_contents($pipes[2]); fclose($pipes[2]); // It is important that you close any pipes before calling // proc_close in order to avoID a deadlock $status = proc_close($process); } return array($status,$stdout,$stderr);

与其他像popen或exec这样的函数相比,proc_open有趣的部分是它提供了特定于windows平台的选项,如:

suppress_errors (windows only): suppresses errors generated by this function when it\’s set to TRUE bypass_shell (windows only): bypass cmd.exe shell when set to TRUE

我知道我选择了答案,因为我必须这样做,但我仍然不明白为什么它不会在我的macchine上工作,但我发现使用另一种方法的回退(使用proc_open)解决方案:

public static function exec_alt($cmd) { exec($cmd,$output); if (!$output) { /** * FIXME: for some reason exec() returns empty output array @mine,\’s machine. * Somehow proc_open() approach (below) works,but doesn\’t work at * test machines – same empty output with both pipes and temporary * files (not we bypass shell wrapper). So use it as a fallback. */ $output = array(); $handle = proc_open($cmd,array(1 => array(\’pipe\’,\’w\’)),$pipes,null,array(\’bypass_shell\’ => true)); if (is_resource($handle)) { $output = explode(\”n\”,stream_get_contents($pipes[1])); fclose($pipes[1]); proc_close($handle); } } return $output; }

希望这有助于某人。

我是同样的问题,花了大量的时间和一杯咖啡

只需在windows 7短路径禁用用户帐户控制(UAC):P C: windows System32 UserAccountControlSettings.exe并选择“从不通知”

和PHP的exec()工作正常!

我使用:Apache版本:2.2.11

PHP版本:5.2.8

windows 7 SP1 32位

最好的祝福! :d

出于安全考虑,您的服务器可能会限制访问“exec”命令。 在这种情况下,也许你应该联系托管公司,以消除这个限制(如果有的话)。

您可以检查IIS用户cmd.exe权限

cacls C:windowssystem32cmd.exe

如果在输出行是像(COmpuTERname =您的计算机名称):

C:windowssystem32cmd.exe COmpuTERnameIUSR_COmpuTERname:N

这意味着用户无权执行cmd。

您可以使用命令添加执行权限:

cacls C:windowssystem32cmd.exe /E /G COmpuTERnameIUSR_COmpuTERname:R

尝试这个:

shell_exec(\’dir 2>&1\’);

它启用UAC的windows 8.1 64位正常工作。

如果你在windows机器上,并尝试像这样运行一个可执行文件:

shell_exec(\”C:ProgramsSomeDirDirinDir..DirWithYourfileYourfile.exe\”);

并没有输出或您的文件没有启动,那么你应该试试这个:

chdir(\”C:ProgramsSomeDirDirinDir..DirWithYourfile\”); shell_exec(\”Yourfile.exe\”);

它应该工作。

如果正在使用脚本当前所在的文件夹的相对路径,这特别方便,例如:

$dir = dirname(__file__).\’\\..\\..\\..\\web\\\’;

如果您需要运行其他程序,请不要忘记将chdir()返回到原始文件夹。

看看Apache的error_log,也许你会发现错误信息。

另外,你可以尝试使用popen而不是exec。 它提供了更多的控制,因为你可以分开进程从输出读取开始:

$p = popen(\”dir\”,\”r\”); if (!$p) exit(\”Cannot run command.n\”); while (!feof($p)) echo fgets($p); pclose($p);

总结

以上是内存溢出为你收集整理的php exec:不返回输出全部内容,希望文章能够帮你解决php exec:不返回输出所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

© 版权声明
THE END
喜欢就支持一下吧
点赞153 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容