安装了多个PHP版本的共享主机上的后台脚本 [英] Background script on shared host with multiple PHP versions installed

查看:190
本文介绍了安装了多个PHP版本的共享主机上的后台脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来为图像管理脚本生成缩略图(使用PHP5),并且我的主机安装了多个版本的PHP(4和5),并将PHP4设置为默认值。这意味着从CLI调用php将会运行PHP4。我想出了以下内容作为我希望成为跨平台解决方案的内容。我在这里发帖主要是因为我在使用谷歌时遇到了很多麻烦,所以这可能会对将来有所帮助,我也有以下问题。

I was needing a way to generate thumbnails (using PHP5) for an image management script and had a problem where my host has multiple versions of PHP installed (4 and 5), with PHP4 set as default. This meant that any calls to php from the CLI would run PHP4. I've come up with the following as what I hope to be a cross platform solution. I'm posting it here primarily as I had a lot of trouble finding any help using Google, so this might help someone in the future, I also have the following questions.


  1. 你看到它有什么明显的错误吗?

  2. 你知道或知道更好的订单有没有任何其他路径到php5二进制文件用于优化的数组?

  3. 如果主机禁用了exec或shell_exec,那么EGalleryProcessQueue.php脚本是否可以作为独立的cron作业运行?我无法访问cron以便能够测试它。我并不太担心这个问题,因为我最终还是会去测试它。

  4. 有没有人对我能得到一些反馈的方式有任何建议通过图像处理的距离是多少?请参阅EGalleryProcessQueue.php的TODO部分。我想在管理部分显示进度条。

  1. Do you see anything obviously wrong with it?
  2. Are there any other paths to the php5 binary that you know of or know of a better order to have the array for optimisation?
  3. If a host has exec or shell_exec disabled, will the EGalleryProcessQueue.php script be able to be run as a standalone cron job? I don't have access to cron to be able to test this yet. I'm not too worried about this question, as I'll get around to testing it eventually anyway.
  4. Does anyone have any suggestions as to a way in which I can get some feedback as to how far through the images the processing is? See the TODO section of EGalleryProcessQueue.php I'd like to display a progress bar when it's in the admin section.

主要脚本

/**
 * Writes the array to a text file in /path/to/gallery/needsThumbs.txt for batch processing.
 * Runs the thumbnail generator script in the background.
 *
 * @param array $_needsThumbs the array of images needing thumbnails
 */
private function generateThumbnails($_needsThumbs)
{
    file_put_contents($this->_realpath.DIRECTORY_SEPARATOR.'needsThumbs.txt',serialize($_needsThumbs));

    $Command = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'EGalleryProcessQueue.php '.$this->_realpath.' '.$this->thumbnailWidth.' '.$this->thumbnailHeight;

    if(PHP_SHLIB_SUFFIX == 'so')// *nix (aka NOT windows)
    {
        /*
         * We need to make sure we are using the right PHP version
         * (problems with shared hosts that have PHP4 and PHP5 installed,
         * but PHP4 set as default).
         */
        $phpPaths = array('php', '/usr/local/bin/php', '/usr/local/php5/bin/php', '/usr/bin/php', '/usr/bin/php5');
        foreach($phpPaths as $path)
        {
            exec("echo '<?php echo version_compare(PHP_VERSION, \"5.0.0\", \">=\"); ?>' | $path", $result);
            if($result)
            {
                shell_exec("nohup $path $Command 2> /dev/null > /dev/null &");
                break;
            }
        }
    }
    else // Windows
    {
        $WshShell = new COM("WScript.Shell");
        $WshShell->Run("php.exe $Command", 0, false);
    }
}

EGalleryProcessQueue.php

EGalleryProcessQueue.php

#!/usr/bin/php
<?php

if ($argc === 4 && strstr($argv[0], basename(__FILE__))) {
    // File is being called by the CLI and has not been included by another script

    if(!file_exists($argv[1].DIRECTORY_SEPARATOR.'needsThumbs.txt'))
    {
        // Either no thumbnails need to be created or a wrong directory has been supplied
        exit;
    }

    include(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'EGalleryThumbGenerator.php');

    $generator = new EGalleryThumbGenerator;
    $generator->directory = $argv[1];
    $generator->thumbnailWidth = is_int($argv[2]) ? $argv[2] : 128;
    $generator->thumbnailHeight = is_int($argv[3]) ? $argv[3] : 128;

    // $generator->processImages() returns the number of images left to process (it does them in blocks of 10)
    while (($i = $generator->processImages()) > 0)
    {
        /*
         * TODO Can we get some sort of feedback to the user here?
         * Possibly so that we can display a progress bar in the management section.
         * Probably have to write $i to a file to be read by the main script.
         */
    }

    exit;
}
?>


推荐答案


你看到了什么显然是错的吗?

Do you see anything obviously wrong with it?

不,代码看起来不错。


你知道或知道更好的订单的php5二进制文件的其他路径是否有优化的数组?

Are there any other paths to the php5 binary that you know of or know of a better order to have the array for optimization?

这是一个很难回答的问题,因为PHP可以安装在服务器上的任何位置。您拥有的路径似乎对我来说非常符合逻辑,但可以安装任意数量的其他地方。

This is a hard question to answer, as PHP could be installed anywhere on a server. The paths you have seem to be very logical to me, but there could be any number of other places it could be installed.

不是提供一堆可能安装PHP5的目录,而是要设置一个参数,用户必须设置该参数以提供PHP5可执行文件的路径,如果它不在他们的$ PATH?

Rather than providing a bunch of directories where PHP5 might be installed, what about having a parameter the user has to set to provide the path to the PHP5 executable if it's not in their $PATH?


如果主机禁用了exec或shell_exec,那么EGalleryProcessQueue.php脚本是否能够通过cron作业运行?

If a host has exec or shell_exec disabled, will the EGalleryProcessQueue.php script be able to be run via a cron job?

我还没有测试过,但我认为会阻止脚本运行。

I haven't tested it, but I would assume that would prevent the script from running.


有没有人有任何建议可以让我得到一些反馈,知道处理的图像有多远?请参阅EGalleryProcessQueue.php的TODO部分。我想在管理部分显示进度条。

Does anyone have any suggestions as to a way in which I can get some feedback as to how far through the images the processing is? See the TODO section of EGalleryProcessQueue.php I'd like to display a progress bar when it's in the admin section.

存储号码在某处完成的图像(文件,数据库,甚至是会话变量)并且每隔一秒左右就有一个AJAX调用触发一个提供完成与总数的函数。然后使用 http://docs.jquery.com/UI/Progressbar

Store the number of images completed somewhere (file, db, maybe even session var) and have an AJAX call fire every second or so to a function that provides done vs total. Then use something like http://docs.jquery.com/UI/Progressbar

这篇关于安装了多个PHP版本的共享主机上的后台脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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