Home arrow PHP arrow Page 4 - PHP: Creating Word Documents without .COM Objects

PHP Script: Validation, File get Contents and Writing to .doc file - PHP

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.

TABLE OF CONTENTS:
  1. PHP: Creating Word Documents without .COM Objects
  2. Strategy for creating this application
  3. The Basic Web Form Source Code
  4. PHP Script: Validation, File get Contents and Writing to .doc file
  5. Deleting Temporary Files in the Server Upload directory
By: Codex-M
Rating: starstarstarstarstar / 5
July 26, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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>



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: