1

I am using CodeIgniter for uploading word documents. I am using read_file for reading word document.

public function open_files(){
    $data['string'] = read_file('./uploads/uwtest.docx');
    $this->load->view('openfiles',$data);       
}

But I am getting different format as output.Something like this

PK,��H]m�Cd word/media/section_image1.jpg��gTSͷ�C�� �� H�  
�tA�z�ދґ"�t����%1�^C/��I@ ��۾���g��5��>g=�33kֹ��Z0�hhk���/�� 
�U�sv��B��:w@r="�n���<@ 
@L�W#�_����d��$��d����T�ם����������G�4Դ'_������QS�SP��u�p�`
kanav
  • 11
  • 3
  • Sure, this is expected behavior. You are just opening the Word file and looking at its binary content. – Lorenz Meyer Apr 14 '17 at 09:28
  • docs is a binary format, you cannot just read it as a text file, need to parse: http://stackoverflow.com/questions/10646445/read-word-document-in-php – Alex Apr 14 '17 at 09:28
  • What exactly are you trying to do with that Word document? – Lorenz Meyer Apr 14 '17 at 09:29
  • I need to add annotations on the word document.For example, someone uploads the document and other users add their analysis on the different part of the word document. – kanav Apr 14 '17 at 09:33

1 Answers1

0

so far, I get this only

function read_docx($filename){
 $striped_content = '';
 $content = '';
 if(!$filename || !file_exists($filename)) return false;

 $zip = zip_open($filename);
 if (!$zip || is_numeric($zip)) return false;

 while ($zip_entry = zip_read($zip)) {

    if (zip_entry_open($zip, $zip_entry) == FALSE) continue;

    if (zip_entry_name($zip_entry) != "word/document.xml") continue;

  $content.=zip_entry_read($zip_entry,zip_entry_filesize($zip_entry));

    zip_entry_close($zip_entry);
}
 zip_close($zip);      
 $content = str_replace('</w:r></w:p></w:tc><w:tc>', " ", $content);
 $content = str_replace('</w:r></w:p>', "\r\n", $content);
 $striped_content = strip_tags($content);

 return $striped_content;
}

But it will return only content.I am looking forward to how to get images and same formatting as in word.

kanav
  • 11
  • 3