php exec()函数和不同的主机 [英] php exec() function and different hosts

查看:67
本文介绍了php exec()函数和不同的主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,可以在几个目录中执行文件,如下所示:

I have a script that executes a file in a couple of directories down which looks like this:

exec("php-cli $file_path > /dev/null 2>/dev/null &"); //php command-line

此功能适用于大多数主机,但有些主机不喜欢它不会运行,不会产生任何错误。

This works on most hosts but some hosts don't like it and it doesn't run, without generating any error.

对于失败的主机,我使用

For the hosts that this fails on, i use

exec("php $file_path > /dev/null 2>/dev/null &"); //notice the -cli is gone

哪个工作正常。

$ file_path是要执行的文件的完整路径/home/blah/path/blah.php

$file_path is the full path to the file being executed /home/blah/path/blah.php

我如何使它统一可以在所有服务器上使用(至少是unix)

How can i make this uniform so it works on all servers (unix at least)

** EDIT * *

**EDIT**

好吧,我是这样做的,这(很可能)不是正确的方法,但是它可以工作。

Well, I'm doing it this way, it is (most likely) not the correct way but it works.

我不仅使用php-cli或php,还同时使用了两者,因此如果其中一个失败,则另一个会通过。 php以cgi或cli的形式运行,并且其中之一将被捕获,并且由于没有输出,因此也不会出现错误。

Instead of just using php-cli or php, i use both so if one fails, the other goes through. Either the php is running as cgi or cli and one of these will catch, and since there is no output, there would be no error either.

exec("php-cli $file_path > /dev/null 2>/dev/null &");
exec("php $file_path > /dev/null 2>/dev/null &");

除非您知道更好的解决方案,否则请告诉我。谢谢您的所有帮助。

Unless you know a better solution please let me know. Thanks for all your help.

推荐答案

function php_exec($file_path) {
    if (!($binary = which(array('php', 'php5', 'php-cli', 'php-cgi'))))
        return false;

    return exec("$binary $file_path > /dev/null 2>/dev/null &");
}

function which($binaries) {
    if (!($path = getenv('PATH')) && !($path = getenv('Path')))
        return false;

    $arr = preg_split('/[:;]/', $path);

    foreach ($arr as $p) {
        foreach ($binaries as $b) {
            if (file_exists("$p/$b"))
                return "$p/$b";
        }
    }   

    return false;
}

var_dump(php_exec('test.php'));

说明:
在大多数系统上,PHP二进制文件称为php,php5,php-cli或php-cgi。 which()函数检查每个名称的标准路径(* nix和Windows都具有称为PATH / Path的环境变量),如果找到具有该名称的文件,它将返回该文件。

Explanation: On most systems the PHP binary is called php, php5, php-cli or php-cgi. which() function checks the standard path (both *nix and windows have environment variable called PATH/Path) for each of those names and if it find a file with such name, it will return it.

PATH变量的格式为: / bin:/ usr / bin:/ usr / local / bin:/ sbin:/ usr / sbin:/ usr / local / sbin 用于* nix(bash),而 C:\Windows\System32\; C:\Windows\; 对于Windows,因此为什么我使用preg_split('/ [:;] /')

The PATH variable's format is: /bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin for *nix (bash), and C:\Windows\System32\;C:\Windows\; for windows, so that's why I use preg_split('/[:;]/')

此解决方案比您的解决方案更好,因为php_exec()将返回 false 如果找不到有效的PHP二进制文件。在您的解决方案中,无法知道脚本执行是否失败。

This solution is better than yours, because php_exec() will return false if it can't find a valid php binary. In your solution there's no way to know if the script execution failed.

这篇关于php exec()函数和不同的主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆