I have been trying recently to open a zip archive, select a particular file within the archive, open it, and access its day to be displayed within a web page.

I have managed to open the zip archive and select the specified file, which is an XML file type.

However, I am finding it quite difficult to make the jump from working within the zip functions to the XML functions provided within PHP.

My goal is to create a dynamic webpage that will do the following: read all files within the directory (successfully accomplished), determine which are sub-directories, files, and display only a selected file type (successfully accomplished); and display specific data contained within the displayed files.

To provide more information on the project, the files located within the directory I'm working within are e-book files, of EPUB format, which is essentially a ZIP archive with a different extension. I wish to open the EPUB, locate "content.opf" and extract its contents for parsing into a user-friendly display.

I can access the file without accessing anything else. I can even access the data contained within as evidenced by the source code of the webpage, showing that the XML data has been loaded into the page as it appears in the original, compressed file. I can't however figure out how to make use of this data. I've been using various XML functions but none of them seem to accept the idea of opening a file located inside a zip archive.

I realize that this is a little more advanced than the usual "newbie" fare but I'm pulling information from google searches and cobbling it together so, I'm still very "newb-ish".

Has anyone attempted to do something similar to this?

Code (strictly for access of XML document) is as follows:

function zipopener()
{
	$zip = zip_open('title.epub');
	if ($zip)
	{
		while ($zip_entry = zip_read($zip))
		{
			if (zip_entry_name($zip_entry) == 'OEBPS/content.opf')
			{
				if (zip_entry_open($zip, $zip_entry))
				{
					echo "File Contents:<br/>";
					$contents = zip_entry_read($zip_entry);
					echo "$contents<br />";

				echo "<br /><br />";


				zip_entry_close($zip_entry);
			}
			echo "</p>";
		}
	}
}
}

if ($folder = opendir('.'))
{
	zipopener();
    closedir($folder);
}

    You could use php's built in DOM functionality, and as a starting point: DOMDocument::loadXML($source) which loads an XML string (while ::load($source) loads XML from a file).
    SimpleXML has a similar function to load strings, simplexml_load_string

    See xml manipulation documentation for various options.

      Thank you for the advice, but actually I found a way around it.

      I discovered that using preg_match_all I could search for the element tag I wanted the information from and retrieve it from the array it stored it in.

      I will however keep your suggestion in mind should I attempt to modify my project.

      The ultimate result is a webpage (XML) that when viewed in the mobile version of "Stanza" the ebook reader that presents the user with a nice list of all the ebooks in a directory and links to any subdirectory. Place this file in each folder within your selected ebook library location and it will allow for navigation from the top level down through any subsequent directories it contains.

      With a modification of the //OUTPUT block, a user can create whatever webpage he or she wishes containing the information found within each ebook. VERY useful if one has a large library of ebooks and wishes to make sure each epub contains the correct data at a glance rather than opening each individually.

      Working on a method to extract the cover image and display it on the side in the same manner Calibre functions, though it may be too resource heavy when put into use.

      Here's the code I've ultimately created:

      <?xml version="1.0" encoding="UTF-8"?>
      <feed xmlns="http://www.w3.org/2005/Atom" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:opds="http://opds-spec.org/2010/catalog">
        <title>YOUR EBOOK LIBRARY NAME HERE</title>
        <id>YOUR SELECTED ID HERE</id>
        <content type="text">YOUR SELECTED VISIBLE CONTENT HERE</content>
        <author>
          <name>YOUR NAME HERE</name>
          <uri>YOUR SELECTED WEB ADDRESS HERE</uri>
          <email>YOUR E-MAIL ADDRESS HERE</email>
        </author>
        <link href="../catalog.xml" type="application/atom+xml;type=feed;profile=opds-catalog" rel="start" title="Main catalog" />
      
      <?php
      // READS THE CONTENTS OF A DIRECTORY
      // AND STRIPS OUT '.' AND '..'
      // AND REPLACES '_' WITH ' ' FOR SERVERS THAT CANNOT USE SPACES IN FILENAMES
      function dir_list($folder) {
        while (false !== ($file = readdir($folder)))  {
          if ($file != "." && $file != "..") {
            if (is_dir($file)) {
              $title = str_replace("_", " ", $file);
      
          // DIRECTORY HEADING
          echo "  <entry>\n";
          echo "    <title>$title</title>\n";
          echo "    <id>$title</id>\n";
          // REPLACE "_BW-Catalog.php" WITH WHAT YOU CHOOSE TO NAME THE FILE
          // THIS SAME FILE CAN BE COPIED AND PASTED INTO SUBDIRECTORIES SO THAT STANZA CAN LINK THEM TOGETHER
          echo "    <link href=\"$file/_BW-Catalog.php\" type=\"application/atom+xml;type=feed;profile=opds-catalog\" />\n";
          echo "  </entry>\n\n";
        }
      }
        }
        // END dir_list()
      }
      
      // READS THE DIRECTORY, STRIPS OUT '.' AND '..', AND CHECKS TO SEE IF
      // 'epub' IS CONTAINED IN THE FILENAME. IF YES, IT OPENS IT, IF NO,
      // IT DOES NOT DISPLAY IT IN THE LIST
      function file_list($folder) {
        $folder = opendir('.');
        while (false !== ($file = readdir($folder))) {
          if ($file != "." && $file != "..") {
            $pos = strpos($file, 'epub');
            if ($pos !== False)
              {zipopener($file);}
          }
        }
        // END file_list()
      }
      
      // OPENS AN EPUB FILE AND SEARCHES FOR 'OEBPS/content.opf'
      function zipopener($file) {
        $EMAIL = "YOUR EMAIL ADDRESS HERE";
        $URL   = "YOUR SELECTED WEB ADDRESS HERE";
        $zip = zip_open($file);
        if ($zip) {
          while ($zip_entry = zip_read($zip)) {
            if (zip_entry_name($zip_entry) == 'OEBPS/content.opf') {
              if (zip_entry_open($zip, $zip_entry)) {
                $contents = zip_entry_read($zip_entry);
      
            // TITLE - EXTRACTS FROM content.opf
            preg_match_all("/<dc:title>(.*?)<\/dc:title>/", $contents, $titlearray, PREG_PATTERN_ORDER);
            $title = $titlearray[0][0];
            $id = strtolower($title);
            $id = str_replace(" ", "", $id);
      
            //AUTHOR - EXTRACTS FROM content.opf
            preg_match_all("/<dc:creator(.*?)<\/dc:creator>/", $contents, $authorarray, PREG_PATTERN_ORDER);
            $author = $authorarray[0][0];
      
            // KEYWORDS - EXTRACTS FROM content.opf
            preg_match_all("/<dc:subject>(.*?)<\/dc:subject>/", $contents, $subjectarray, PREG_PATTERN_ORDER);
            // IF THERE ARE ANY KEYWORDS, THIS PLACES THEM IN A SINGLE DIMENSION ARRAY FOR LATER USE
            // CURRENTLY UNUSED IN THIS FORM OF THE PROJECT
            foreach ($subjectarray[0] as $value)
              {$keywords[] = $value;}
      
            zip_entry_close($zip_entry);
          }
        }
      }
        }
      
        // OUTPUT - GENERATES AN XML FILE TO BE READ BY STANZA AS AN EBOOK LIBRARY
        echo "  <entry>\n";
        echo "    <title>$title</title>\n";
        echo "    <id>$title</id>\n";
        echo "    <link href=\"$file\" type=\"application/epub+zip\" rel=\"http://opds-spec.org/acquisition\" title=\"Download this ebook as EPUB\" />\n";
        echo "    <author>\n";
        echo "      <name>$author</name>\n";
        echo "      <uri>$URL</uri>\n";
        echo "      <email>$EMAIL</email>\n";
        echo "    </author>\n";                
      echo " </entry>\n\n"; //END zipopener() } if ($folder = opendir('.')) { // GIVES A LIST OF DIRECTORIES dir_list($folder); // GIVES A LIST OF E-BOOKS file_list($folder); // CLOSES THE ARCHIVE closedir($folder); } ?> </feed>
        Write a Reply...