运行一个广泛的PHP脚本 - 编写XML


Running an extensive PHP script - writes XML

我有一个相当广泛的PHP脚本,我必须在我的数据库上运行它,并从文件结构中收集img大小并将该信息写入XML文件。我尝试直接从 Web 浏览器运行,但收效甚微,因为网关在大约 3 分钟后超时。我也尝试从 ssh 外壳运行,但没有成功。这是大部分脚本

    //Get all galleries from client
            $query = mysql_query("SELECT *
                                    FROM gallery
                                    WHERE clientRef BETWEEN 420
                                    AND 430
                                    ORDER BY clientRef
                                ");
            while ($row = mysql_fetch_array($query)) {
            //set gallery id
            $galleryID =  $row['id'];
            $clientRef = $row['clientRef'];
        //Check image sizes and set horizontal vs vertical
            $fImageSizeName = mysql_fetch_array($qAlbumImages);
            $imageSizeName = $fImageSizeName['OrgImageName'];
            $galleryPath = $_SERVER['DOCUMENT_ROOT'] . "/data/gallery/" . $galleryID . "/images/"; 
            if(is_dir($galleryPath)) {
                list($width, $height) = getimagesize($_SERVER['DOCUMENT_ROOT'] . "/data/gallery/" . $galleryID . "/images/album/" . $imageSizeName);
    header("Content-Type: text/plain");

        //Create the xml document
        $xmlDoc = new DOMDocument();
        //Create the root Element
        $root = $xmlDoc->appendChild(
                $xmlDoc->createElement("PageflipDataSet"));
        //Create settings element
        $settings = $root->appendChild(
                    $xmlDoc->createElement("Settings"));
    //Create PageOrder Node
        $PageOrder = $root->appendChild(
                    $xmlDoc->createElement("PageOrder"));
        $qAlbumImages = mysql_query("   SELECT *
                                        FROM galleryimage 
                                        WHERE galleryId='{$galleryID}' 
                                        AND clientRef= '{$clientRef}' 
                                        ORDER BY sort
                                    ");
        while ($fAlbumImages = mysql_fetch_array($qAlbumImages)) {
            $path  = "../../../../data/gallery/" . $galleryID . "/images/album/" . $fAlbumImages['OrgImageName'];
            //Create the PageData Node
            $PageData = $PageOrder->appendChild(
                        $xmlDoc->createElement("PageData"));
                //PageFile attribute
                $PageData->appendChild(
                        $xmlDoc->createAttribute("PageFile"))->appendChild(
                        $xmlDoc->createTextNode($path));
        }//close while
//Format output so it looks pretty
    $xmlDoc->formatOutput = true;
    //Save and create path to gallery ID
    $galleryPath = $_SERVER['DOCUMENT_ROOT'] . "/data/gallery/" . $galleryID;
    //Set path
    if(is_dir($galleryPath)) {
        $xmlPath = $_SERVER['DOCUMENT_ROOT'] . '/data/gallery/' . $galleryID . '/xml/pageflipdata.xml';
        $xmlDoc->save($xmlPath);
    }
}//close while

关于我应该如何做到这一点的任何建议。大约有 2000 个 clientRef,每个 clientRef 有 5-10 个相册,可以包含 15-30 张图像。

如果超时是主要问题,您可以以较小的块进行,稍后手动组合 xml。

如果需要定期运行,您可以使用类似的过程,但使用 ajax 或等待所有小块脚本的确认以运行 xml 组合脚本的不同请求中的每个"较小块"。

当然,简单地增加PHP的max_execution_time会更容易。如果您无权访问此功能,则可以通过监视执行时间,然后调用 set-time-limit 再次重置计时器来将某些内容调整为工作顺序。不过,我从未尝试过这个,所以只是一个想法。