PHP读取zip中的所有文本文件并将其保存为字符串变量

时间:2012-08-10 16:04:28

标签: php xml text zip

你好专业人士我再次来到这里寻求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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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);
  }
}