Read MS word (docx / MS word 2007 file) document using php

I was working in job portal project, in job portal website, we give options to jobseekers upload their resume in .doc, .rtf, .txt and .pdf formats.

We’re getting lots of request from our users for uploading resumes in .docx file format. after we decided to allow upload their resumes in .docx file format. At that time I don’t know how to read the docx file using php. Finally I read docx file using php. Here i place the read docx file using php code. Use this code and read docx file using php.

	function read_file_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));

		}// end while


		//echo $content;
		//echo "<hr>";
		//file_put_contents('1.xml', $content);		

		$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;

Usage #1:


$filename = "file.docx";// or /var/www/html/file.docx

	$content = read_file_docx($filename);
	if($content !== false) {

		echo nl2br($content);
	else {
		echo 'Couldn\'t the file. Please check that file.';

Click “download” link and get the sample code.

Popularity: 60% [?]

Related posts:

  1. Read xlsx files in php
  2. How to send email in php / Send email with attachment in php
  3. How to validate email in php with/without MX record
  4. How to solve the “header already sent” problem in php
  5. How can I import data in mysql server from mysql dump file, SQL data file

3 thoughts on “Read MS word (docx / MS word 2007 file) document using php

  1. hey,
    i tried to user your code, with the its not working
    zip_open function is returning me 11. so can u please help me out what else it needed to make your code work.

Leave a Reply

Your email address will not be published. Required fields are marked *


four + 2 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>