0

您好专业人士,我再次来到这里寻求 php 编程方面的帮助。我对这门语言真的很陌生,但学得很多。纳夫说。

无论如何,我现在遇到了困难,我想读取一个 zip 文件,其中包含许多包含文本文件的文件夹,并保存在字符串变量(不是文本文件的名称!)中,即文本文件本身的内容。这将为我提供完成任务的示例。

具体来说,我实际上是在尝试读取 zip 中的所有 xml 文件。但是文本文件的示例会很好。

这是我目前拥有的:

<?php

function comment(){

    $moodle = new Moodle();

    $zip = zip_open('qwerty.zip');


    if ($zip)
    {
        while ($zip_entry = zip_read($zip))
        {
            //echo "Name: " . zip_entry_name($zip_entry). "<br />";

            $data = zip_entry_read($zip_entry);

            $xml = new SimpleXMLElement($data);

            //echo $data;



        }
        zip_close($zip);
    }




}

comment();

?>

感谢所有在场的人。格拉西亚斯。

更新

这实际上是精确的输出:

Warning: SimpleXMLElement::__construct(): Entity: line 28: parser error : expected '>' in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): <component>mod_resource</compon in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): ^ in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): Entity: line 28: parser error : Opening and ending tag mismatch: component line 28 and compon in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): <component>mod_resource</compon in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): ^ in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): Entity: line 28: parser error : Premature end of data in tag file line 25 in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): <component>mod_resource</compon in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): ^ in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): Entity: line 28: parser error : Premature end of data in tag files line 2 in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): <component>mod_resource</compon in D:\xampp\htdocs\project\index.php on line 47

Warning: SimpleXMLElement::__construct(): ^ in D:\xampp\htdocs\project\index.php on line 47

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in D:\xampp\htdocs\project\index.php:47 Stack trace: #0 D:\xampp\htdocs\project\index.php(47): SimpleXMLElement->__construct('<?xml version="...') #1 D:\xampp\htdocs\project\index.php(85): comment() #2 {main} thrown in D:\xampp\htdocs\project\index.php on line 47
4

2 回答 2

1

上面的代码工作正常。问题在于您的 xml 文件。所有这些错误都来自 xml 验证器。

于 2012-08-10T17:43:23.703 回答
1

我终于明白了。感谢您尝试帮助我。所以我就带着这个来了。

function moodlezip($zipfile) {
  echo "<h1>MOODLE</h1>"."<br />";
  $moodle = new Moodle();

  $zipfile = 'backup-moodle2-course-music_basic-20120806-1359b.mbz';
  $zip = zip_open($zipfile);
  $ziparc = new ZipArchive;

  if ($zip) {
    while ($zip_entry = zip_read($zip)) {
      $file = zip_entry_name($zip_entry);
      //echo "Name: " . $file . "<br />";

      if (strpos($file,'course.xml') !== false) {
        if ($ziparc->open($zipfile) === TRUE) {
          $coursexml =  new SimpleXMLElement($ziparc->getFromName($file));
          $moodle->getCourse($coursexml);
          $ziparc->close();
        } else {
          echo 'failed';
        }
      }
      else if (strpos($file,'forum.xml') !== false) {
        if ($ziparc->open($zipfile) === TRUE) {
          $topicxml =  new SimpleXMLElement($ziparc->getFromName($file));
          $moodle->getTopic($topicxml);
          $ziparc->close();
        } else {
          echo 'failed';
        }
      }
      else if (strpos($file,'questions.xml') !== false) {
        if ($ziparc->open($zipfile) === TRUE) {
          $questionsxml =  new SimpleXMLElement($ziparc->getFromName($file));
          $moodle->getQuestions($questionsxml);
          $ziparc->close();
        } else {
          echo 'failed';
        }
      }
    }
    zip_close($zip);
  }
}
于 2012-08-14T11:33:55.777 回答