PHP: Creating Word Documents without .COM Objects

If you are looking for a solution to create MS Word documents without the use of .COM objects, then this tutorial will be helpful. In it, you will learn how to build an actual PHP application that can convert a .txt file into a .doc file online.

Important Requirements

Before you implement this application in your own test server, bear in mind that this was tested to work on the following setup:

  • PHP Version: 5.2.5
  • Server: Apache
  • OS: Linux
  • MS Word version: MS Word 2002

You will need the following PHP functions/components to make this work:

  • require_once
  • $_FILES
  • file_get_contents
  • mt_rand
  • fopen
  • fwrite

This web application won’t work if you try to implement it on a free web hosting platform, because hosting agencies will disable some of the functions listed above.

Also you will need to have a recaptcha account; this is the captcha solution that is used for this application. For the recaptcha to work, you will need:

a. Public Key

b. Private Key

c. Recaptcha PHP library files

You can get that on this page: http://www.google.com/recaptcha

It is highly recommended that you test this application first in the local server, such as XAMPP. So make sure you have a PHP-Apache test server installed on your system before uploading files to your web hosting company.

{mospagebreak title=Strategy for creating this application}

Since this application does not use .com objects, an alternative solution focuses mainly on the fopen PHP function. Below is the flowchart of this application:

Suppose you are going to use PHP to convert a .txt file to .doc. It starts by checking to see if the web form has been submitted. If the web form has been submitted, PHP will validate the recaptcha, file type (file uploaded should be of the .txt file type) and file size (for example, if it is less than the 100 KB upload limit).

If there are no errors in this validation, PHP will simply get the content from the uploaded text file, which is placed in the temporary upload directory of the server. If the file is not created, fopen will create the .doc file.

Once the file has been created, PHP will write the contents to an MS Word file using the fwrite command, and finally, present the download link to the client.

To manage the size of the upload directory, and for maintenance, an external PHP script will be regularly executed using web hosting cron tab (http://adminschoice.com/crontab-quick-reference) features. The objective of the script is to check for files in the directory (with the .txt and .doc extensions) which were created more than three minutes ago in the directory. If the files were created more than three minutes ago, those files will be scheduled for deletion by the cron job.

This feature is added to prevent temporary files from clogging the server’s temporary upload directory, to save some disk space.

The entire process looks simple, and this tutorial will guide you through all of these steps in detail.

{mospagebreak title=The Basic Web Form Source Code}

Based on the flowchart, the first major steps are presentation of the web form and recaptcha. Below is the source code for this section:

<HTML>

<HEAD>

<TITLE>Convert Text File (.txt) to MS Word (.doc) using PHP</TITLE>

</HEAD>

<BODY>

<font face="Courier" size="2">

</head>

<body>

<H2>Convert Text File to MS Word</H2>

<br></br>

<b>Background</b>: This will convert your text file (.txt extension) into an MS Word file (.doc extension).The converted version is compatible for MS Word 2002 but it may also work with newer versions of MS Word.

<br /><br />

Bear in mind that files are deleted every 3 to 10 minutes so you need to download the converted file right away.

<br /><br />

Note that after downloading and opening the file, you might encounter these two common errors:

<br />

1. MSWRD632 related errors. In this case, you need to read this solution: <font color="blue">http://social.answers.microsoft.com/Forums/en-US/wordcreate/thread/ce3a65ce-fba1-4c16-af9c-d4131a281527</font>

<br />

2.) Now if another error appears like asking you install a text converter or like that just click "Yes" to proceed to any installation. This will let you open the converted files without any error the next time around.

<br />

<br /><br />

Maximum allowable Text File upload size: <b>100kB</b>

<br /><br />

<!– Input form begin –>

<form NAME="texttoword" ACTION="<? echo $PHP_SELF; ?>" method="post" enctype="multipart/form-data">

<label for="file">Filename:</label>

<br /><br />

<input type="file" name="file" id="file" />

<INPUT TYPE="HIDDEN" NAME="ref" VALUE="texttoword">

<br /><br />

Type the captcha below:

<br /> <br />

<?php

require_once(‘recaptchalib.php’);

$publickey = "***PUT YOUR OWN PUBLIC KEY HERE***";

echo recaptcha_get_html($publickey);

?>

<br /> <br />

<INPUT CLASS="DEFAULT" TYPE=SUBMIT VALUE="Convert this text file to MS Word">

</form>

<br />

<br />

The code looks self-explanatory. It includes user-friendly information that will be useful if the client encounters a problem when opening the converted MS Word file.

Also, the recaptcha code snippet should be placed above the submit button and the </form> tag.

The form should look like the screen shot below:

{mospagebreak title=PHP Script: Validation, File get Contents and Writing to .doc file}

Here is the PHP script to make this application work, complete with comments:

<?php

//First, check if the web form has been submitted by checking the value of $_POST[ref]

if($_POST[ref] == "texttoword") {

//If the form has been submitted, check if the recaptcha entered by the client is correct.

require_once(‘recaptchalib.php’);

$errors=array();

$privatekey = "***PUT YOUR OWN PRIVATE KEY HERE***";

$resp = recaptcha_check_answer ($privatekey,

$_SERVER["REMOTE_ADDR"],

$_POST["recaptcha_challenge_field"],

$_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {

//Display error back to the client if the recaptcha entered is incorrect.

die(‘<font color="red">ERROR: The recaptcha code was not entered correctly or expired. If you think it expired or correctly entered; click Recaptcha refresh button to get a new challenge and press submit again.</font>’);

}

//Check also if the file uploaded by the client is a text file.

//Check also the file size, it should be less than 100kB.

//Accept file input in terms of plain text from the user. Plain text has a file extension of .txt

if (($_FILES["file"]["type"] == "text/plain") && ($_FILES["file"]["size"] < 100000))

{

if ($_FILES["file"]["error"] > 0)

{

echo "Return Code: " . $_FILES["file"]["error"] . "<br />";

}

else

{

if (file_exists("upload/" . $_FILES["file"]["name"]))

{

echo $_FILES["file"]["name"] . " already exists. ";

}

else

{

move_uploaded_file($_FILES["file"]["tmp_name"],

"upload/" . $_FILES["file"]["name"]);

}

}

}

else

{

//Display error back to the client if the file uploaded is invalid.

die(‘<font color="red">Invalid file uploaded.</font>’);

}

//If there are no errors during the validation done above, then get the posted uploaded file and assign to a PHP variable

$filetoread="upload/" . $_FILES["file"]["name"];

//Extract the text content of the text file using file_get_contents PHP function, and then assigned the extracted text to a PHP variable.

$text_content=file_get_contents($filetoread);

//To prevent file duplication conflict in case several users are downloading the converted file at the same time, use a unique random key appended to the doc file.

//Generate random number between 10,000 and 99999

$uniquerandomkey = mt_rand(10000, 99999);

//Append unique random key to the word file name

$wordfilename= ‘convertedtoword’.$uniquerandomkey.’.doc’;

//Create a temporary word file in the upload directory and then prepare this for writing

$fp = fopen("upload/$wordfilename", ‘w+’);

//Assign the text content extracted from file_get_contents to $writetexttowordfile PHP variable

$writetexttowordfile = $text_content;

//Finally write the text content to the newly-created Word file

fwrite($fp, $writetexttowordfile);

//After writing the file, close it.

fclose($fp);

// Now that word file has been created, present the download link to the client.

$file = "upload/$wordfilename";

echo ‘<h3>Download link</h3>’;

echo ‘<br />’;

echo ‘<a rel="nofollow" href="’.$file.’"><font size="2">Right click here then <b>Save as</b> to Download</a></font>’;

echo ‘<br />’;

echo ‘<br />’;

}

//Show error in case the form is not posted.

elseif ($_POST[ref] == "texttoword") {

echo ‘<font color="red">This form is not posted.</font>’;

}

?>

</font>

</body>

</html>

{mospagebreak title=Deleting Temporary Files in the Server Upload directory}

Of course, since you are allowing text and doc files to be uploaded/created, they will clog that directory for some time and consume a lot of disk space. Below is the script that will delete these files (.txt and .doc).

<?php

//This is variation of the public domain source code found here:

// http://www.jonasjohn.de/snippets/php/delete-temporary-files.htm

// Define the folder to clean

// (keep trailing slashes)

// You need to run this script using your web hosting cron tab (consult your web hosting agency for details) at least every 10 minutes.

// This script will only delete files created and uploaded if the file is more than 3 minutes old.

//Define path, YOU NEED TO CHANGE THIS PATH!

$uploadfolder = ‘/home/www/php-developer.org/convert-txt-to-msword/upload/’;

//Delete MS Word files first

$fileTypes1 = ‘*.doc’;

// Define the expiration minutes , default 3 minutes

$expire_time1 = 3;

// Find all MS Word files in the path

foreach (glob($uploadfolder . $fileTypes1) as $Filename1) {

// Read file creation time

$FileCreationTime1 = filectime($Filename1);

// calculate the age of the files.

$FileAge1 = time() – $FileCreationTime1;

// Condition to evaluate age of files

if ($FileAge1 > ($expire_time1 * 60)){

// If the file is more than 3 minutes old, then schedule it to be deleted in the next cron job execution of this script.

unlink($Filename1);

}

}

//Same thing above, but this will delete only text files

$fileTypes2 = ‘*.txt’;

$expire_time2 = 3;

foreach (glob($uploadfolder . $fileTypes2) as $Filename2) {

$FileCreationTime2 = filectime($Filename2);

$FileAge2 = time() – $FileCreationTime2;

if ($FileAge2 > ($expire_time2 * 60)){

unlink($Filename2);

}

}

?>

Implementation and the Complete Script

You can download the complete source code and see an actual implementation here: http://www.php-developer.org/convert-txt-to-msword/

The deletefiles.php is the actual PHP script that you will need to run using your web hosting cron tab. It is recommended that you change the file name from deletefiles.php to another name for security reasons.

After downloading the file, extract it and upload the folder to the root directory of your server.

In the upload folder, an .htaccess has been added to prevent browser execution of text files. This is a security feature in case someone uploads a text file which in reality is not a text file.

You might as well try uploading text files to the actual page here: http://www.php-developer.org/convert-txt-to-msword/ so that you can see how the web application works as planned.

To download the MS Word file, you will simply right click and save as the appropriate file type to your desktop.

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan