Open MS Word Docx File In PHP

This snippet shows how to open a .docx file using the zip functions. This example outputs the entire contents of the word document to the browser without formatting.


  1. <?php
  2. function OpenDocx($filename){
  3.     $zip = zip_open($filename);
  4.     if ($zip){
  5.         while ($zip_entry = zip_read($zip)){
  6.             if (zip_entry_open($zip, $zip_entry)){
  7.                 if (zip_entry_name($zip_entry) =="word/document.xml"){
  8.                     $content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  9.                     zip_entry_close($zip_entry);
  10.                 }
  11.             }
  12.         }
  13.         $striped_content = strip_tags($content);
  14.         zip_close($zip);
  15.     }
  16.     return $striped_content;
  17. }
  18. $filename = dirname(__FILE__) . "/cv1.docx";
  19. echo OpenDocx($filename);
  20. ?>

No comments:

Post a Comment