PHP:将目录和子目录中的文件列表到XML文件中

问题描述 投票:1回答:1

[[编辑:固定的错别字和更新的脚本]

你好,

我正在尝试列出存储在远程服务器上的文件,并将该列表存储到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";
}

?>

谢谢。

php xml scandir
1个回答
0
投票

[当您重复某些操作时,我整理了很多代码,我也减少了代码,以便在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";
}
© www.soinside.com 2019 - 2024. All rights reserved.