【问题标题】:PHP : list files in directory and sub-directories into an XML filePHP:将目录和子目录中的文件列出到 XML 文件中
【发布时间】:2020-04-24 12:35:38
【问题描述】:

[编辑:修正错别字并更新脚本]

你好,

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

?>

谢谢。

【问题讨论】:

  • 你的代码有很多错误。语法错误:$dom-createElement($file) 缺少&gt;,在带有$xmlPath 的行中缺少;。您在if (is_dir($file_c)); 上有一个额外的;。尝试使用路径 (createElement($filePath)) 创建元素很可能是无效的。在writeXML($file, $dom) 中,$dom 是一个元素,因此您不能调用createElement(),这需要在文档本身上完成。您创建一个文档片段,因为没有根元素。
  • 尝试在脚本中启用错误(error_reporting(E_ALL); ini_set('display_errors', 1); 在开始时)并解决错误。如果你走得更远,那么它可能会有所帮助。
  • 感谢您指出错别字和启用错误的建议,我几乎都想通了。现在我只需要制作一个函数递归函数。

标签: php xml scandir


【解决方案1】:

当你重复一些事情时,我已经整理了很多代码,我还减少了它,以便在 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";
}

【讨论】:

    猜你喜欢
    • 2018-07-04
    • 1970-01-01
    • 2017-10-08
    • 2011-02-23
    • 2012-11-14
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多