How would you go about reading odt files in PHP? I know you can use QueryPath, but that seems a bit of an overkill,.. I just want to read the file.
Asked
Active
Viewed 8,348 times
4 Answers
5
odt, files are zip compressed xml.
If all you need to do is read the file raw. Just unzip it and read it like a normal file.
If you need to parse out usable text, then enters the need for QueryPath or some other xslt parser.

Harmon Wood
- 2,977
- 1
- 15
- 12
-
I have no idea how I'm going to code that.. use zip_open, then zip_read,.. then file_get_contents? – john mossel Nov 01 '10 at 17:23
-
1Here is a great little article doing the whole thing in 10 lines of code. http://technosophos.com/content/reading-odt-files-querypath – Harmon Wood Nov 02 '10 at 18:34
-
Arr,... all I need to do was change some file permissions, nothing wrong with that, sorry – john mossel Nov 13 '10 at 22:40
1
/*Name of the document file*/
$document = 'Template.odt';
/**Function to extract text*/
function extracttext($filename) {
$dataFile = "content.xml";
//Create a new ZIP archive object
$zip = new ZipArchive;
// Open the archive file
if (true === $zip->open($filename)) {
// If successful, search for the data file in the archive
if (($index = $zip->locateName($dataFile)) !== false) {
// Index found! Now read it to a string
$text = $zip->getFromIndex($index);
// Load XML from a string
// Ignore errors and warnings
$xml = new DOMDocument;
$xml->loadXML($text, LIBXML_NOENT | LIBXML_XINCLUDE | LIBXML_NOERROR | LIBXML_NOWARNING);
// Return XML
return $xml->saveXML();
}
//Close the archive file
$zip->close();
}
// In case of failure return a message
return "File no`enter code here`t found";
}
echo extracttext($document);

Nishant Bhatt
- 509
- 5
- 8
0
http://pear.php.net/package/OpenDocument may be what you need. Haven't used it myself, though.

Olivier Berger
- 21
- 1