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>
blog comments powered by Disqus |
|
|
|
|
|
|
|