PHP : 列出目录和子目录中的文件到一个 XML 文件中

PHP : list files in directory and sub-directories into an XML file

[编辑:修复拼写错误并更新脚本]

你好,

我正在尝试列出存储在远程服务器上的文件并将该列表存储到一个 XML 文件中,该文件将在软件更新程序中使用。 为此,我编写了一个递归函数,它针对每个文件调用另一个函数来写入所有必要的信息(文件名、md5 哈希和路径)。

但是,当我需要该功能转到文件树的底部时,它似乎只列出了第一组子目录。

这是我使用的代码,如果有人能告诉我我缺少什么?

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

function writeXml($pathInfo)
{
    if(!file_exists("file_listing.xml"))
            {
                //XML creation
                $dom = new DomDocument("1.0", "ISO-8859-15");
                $dom->preserveWhiteSpace = false;
                $dom->formatOutput = true;

                $root = $dom->createElement("FileList");
                $dom->appendChild($root);

                echo "Treating ".$pathInfo['basename']." in ".$pathInfo['dirname']."<br>";
                $xmlNodeFile = $dom->createElement("Name");
                $xmlNodeFileText = $dom->createTextNode($pathInfo['basename']);
                $root->appendChild($xmlNodeFile);
                $xmlNodeFile->appendChild($xmlNodeFileText);

                $xmlPath = $dom->createElement("Path");
                $xmlPathText = $dom->createTextNode($pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']);
                $xmlNodeFile->appendChild($xmlPath);
                $xmlPath->appendChild($xmlPathText);
                echo "----Directory : ".$pathInfo['dirname']."<br>";

                $xmlMd5 = $dom->createElement("MD5");
                $fileMd5 = md5_file($pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']);
                $xmlMd5Text = $dom->createTextNode($fileMd5);
                $xmlNodeFile->appendChild($xmlMd5);
                $xmlMd5->appendChild($xmlMd5Text);
                echo "----MD5 : ".$fileMd5."<br>";

                $xmlSha1 = $dom->createElement("SHA1");
                $fileSha1 = sha1_file($pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']);
                $xmlSha1Text = $dom->createTextNode($fileSha1);
                $xmlNodeFile->appendChild($xmlSha1);
                $xmlSha1->appendChild($xmlSha1Text);
                echo "----SHA1 : ".$fileSha1."<br>";

                $dom->save('file_listing.xml');
                echo $pathInfo['basename']." written. <br><br>";
            }
            elseif(file_exists("file_listing.xml"))
            {
                //XML loading
                $dom = new DomDocument();
                $dom->preserveWhiteSpace = false;
                $dom->formatOutput = true;
                $dom->load("file_listing.xml", LIBXML_PARSEHUGE);

                $root = $dom->documentElement;

                echo "Treating ".$pathInfo['basename']." in ".$pathInfo['dirname']."<br>";
                $xmlNodeFile = $dom->createElement("Name");
                $xmlNodeFileText = $dom->createTextNode($pathInfo['basename']);
                $root->appendChild($xmlNodeFile);
                $xmlNodeFile->appendChild($xmlNodeFileText);

                $xmlPath = $dom->createElement("Path");
                $xmlPathText = $dom->createTextNode($pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']);
                $xmlNodeFile->appendChild($xmlPath);
                $xmlPath->appendChild($xmlPathText);
                echo "----Directory : ".$pathInfo['dirname']."<br>";

                $xmlMd5 = $dom->createElement("MD5");
                $fileMd5 = md5_file($pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']);
                $xmlMd5Text = $dom->createTextNode($fileMd5);
                $xmlNodeFile->appendChild($xmlMd5);
                $xmlMd5->appendChild($xmlMd5Text);
                echo "----MD5 : ".$fileMd5."<br>";

                $xmlSha1 = $dom->createElement("SHA1");
                $fileSha1 = sha1_file($pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']);
                $xmlSha1Text = $dom->createTextNode($fileSha1);
                $xmlNodeFile->appendChild($xmlSha1);
                $xmlSha1->appendChild($xmlSha1Text);
                echo "----SHA1 : ".$fileSha1."<br>";

                $dom->save('file_listing.xml');
                echo $pathInfo['basename']." written. <br><br>";
            }

} //endof writeXml function


function scanRecursToXml($dirToScan = '.') //scandir recursive function
{
    //echo "Reading in ".$dirToScan.DIRECTORY_SEPARATOR."<br>";

    $dir = opendir($dirToScan);

    while(false !== ($file = readdir($dir)))
    {   
        //Skip '.' and '..'
        if($file == '.' || $file == '..')
        {
            continue;
        }

        $followUp = $dirToScan.DIRECTORY_SEPARATOR.$file;

        if(!is_dir($file) && $file != 'index.php' && $file !='file_listing.php' && $file !='file_listing.xml')
        {
            echo "Listing ".$file." in ".$dirToScan."<br>";

            $filePathInfos = realpath($followUp);
            writeXml(pathinfo($filePathInfos));        

        }
        elseif (is_dir($followUp))
        {
                scanRecursToXml($followUp);
        }

    }
    closedir($dir);
}   //endof scanRecurs function

//We start the process with a scanRecurs()
$localDir = '.';
scanRecursToXml($localDir);

//In the end, we check the presence of the XML file
if(file_exists('file_listing.xml'))
{
    echo "Done";
}
else
{
    echo "Something went wrong :s";
}

?>

谢谢。

我已经整理了很多代码,因为你重复了一些事情,我也减少了它,所以文档是 created/opened 在 scanRecurs 函数的第一级...

function writeXml(&$node, $pathInfo)
{
    $dom = $node->ownerDocument;
    $file = $dom->createElement("File");
    $node->appendChild($file);

//     echo "Treating ".$pathInfo['basename']." in ".$pathInfo['dirname']."<br>";
    $xmlNodeFile = $dom->createElement("Name", $pathInfo['basename']);
    $file->appendCHild($xmlNodeFile);

    $xmlPath = $dom->createElement("Path", $pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']);
    $file->appendChild($xmlPath);
//     echo "----Directory : ".$pathInfo['dirname']."<br>";

    $fileMd5 = md5_file($pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']);
    $xmlMd5 = $dom->createElement("MD5", $fileMd5);
    $file->appendChild($xmlMd5);
//     echo "----MD5 : ".$fileMd5."<br>";

    $fileSha1 = sha1_file($pathInfo['dirname'].DIRECTORY_SEPARATOR.$pathInfo['basename']);
    $xmlSha1 = $dom->createElement("SHA1", $fileSha1);
    $file->appendChild($xmlSha1);
//     echo "----SHA1 : ".$fileSha1."<br>";

} //endof writeXml function


function scanRecursToXml($dirToScan = '.', $rootNode = null) //scandir recursive function
{
    $newFile = false;
    if ( $rootNode == null )
    {
        $newFile = true;
        $dom = new DomDocument();
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        if(!file_exists("file_listing.xml"))
        {
            //XML creation
            $rootNode = $dom->createElement("FileList");
            $dom->appendChild($rootNode);
            echo "Creating".PHP_EOL;
        }
        else    {
            //XML loading
            $dom->load("file_listing.xml", LIBXML_PARSEHUGE);
            $rootNode = $dom->documentElement;
            echo "Loading".PHP_EOL;
        }
    }
    $dir = opendir($dirToScan);
    while(false !== ($file = readdir($dir)))
    {
        //Skip '.' and '..'
        if($file == '.' || $file == '..')
        {
            continue;
        }

        $followUp = $dirToScan.DIRECTORY_SEPARATOR.$file;

        echo $followUp.PHP_EOL;
        if(!is_dir($followUp) && $followUp != 'index.php' && $followUp !='file_listing.php' && $followUp !='file_listing.xml')
        {
            echo "Listing ".$followUp." in ".$dirToScan."<br>";

            $filePathInfos = realpath($followUp);
            writeXml($rootNode, pathinfo($filePathInfos));

        }
        elseif (is_dir($followUp))
        {
            scanRecursToXml($followUp, $rootNode);
        }

    }
    closedir($dir);
    if ( $newFile )    {
        echo "Saving";
        $dom->save('file_listing.xml');
    }
}   //endof scanRecurs function

//We start the process with a scanRecurs()
$localDir = '.';
scanRecursToXml($localDir);

//In the end, we check the presence of the XML file
if(file_exists('file_listing.xml'))
{
    echo "Done";
}
else
{
    echo "Something went wrong :s";
}