【问题标题】:PHP read all text files in a zip and save it to a string variablePHP 读取 zip 中的所有文本文件并将其保存到字符串变量中
【发布时间】:2012-08-10 16:04:28
【问题描述】:

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

【问题讨论】:

  • 那么你在哪里得到错误?什么不工作?
  • 这就是我自己想要弄清楚的......这就是我得到的。致命错误:在 D:\xampp\htdocs\project\index.php:47 中未捕获的异常 'Exception' 和消息 'String could not be parsed as XML' 堆栈跟踪:#0 D:\xampp\htdocs\project\index。 php(47): SimpleXMLElement->__construct('
  • 听起来字符串不是有效的 XML。
  • 抛出该错误时$data 中的内容是什么?你确定那个 .zip 中只有 xml 文件吗?
  • 我同意该字符串可能不是有效的 XML。对于多目录 zip,上面的代码按预期(递归)工作。

标签: php xml text zip


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    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);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多