Home arrow PHP arrow Page 5 - PHP Form to Text File: the Basics

Adding features, continued - PHP

This is a basic tutorial on the application of PHP to get text information from a web form and write it to a text file.

TABLE OF CONTENTS:
  1. PHP Form to Text File: the Basics
  2. Processing inputs from submitted form and writing the text to a file
  3. Code discussion
  4. Adding Four Features to the Basic Web Application
  5. Adding features, continued
By: Codex-M
Rating: starstarstarstarstar / 3
July 20, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Feature 2. Limiting the number of words that can be written to the file. 

Say that you are using this application for a 500 word essay. To limit the number of words, simply add these lines:  

$count = count(explode(" ", $textinputs));

If ($count>500) {

 

die('ERROR: Your text should be less than 500 words.');

 

}

 

You will need to insert this after the validation of the user inputs are completed.  

Example (added lines in blue font):  

<?php

 

}

 

else

 

{

 

//Form submitted, grab the data from POST

 

$textinputs =trim($_POST['textinputs']);

 

//If you need to sanitize user input

 

$textinputs= strip_tags($textinputs);

 

$textinputs =nl2br($textinputs);

 

$textinputs =htmlspecialchars($textinputs);

 

$textinputs =escapeshellarg($textinputs);

 

$textinputs =substr($textinputs,1,-1);

 

//Test if it contains some data.

 

if (!isset($textinputs) || trim($textinputs) == "")

 

{

 

//Feedback to user that it contains no data

 

die ('ERROR: Enter your text. <a href="/phpformtotextfile">Click here to proceed.</a>');

 

}

 

else

 

{

 

//User input validation Completed

 

//Count the number of words in the text

 

$count = count(explode(" ", $textinputs));

 

If ($count>500) {

 

die('ERROR: Your text should be less than 500 words.');

 

}

 

Features 3 and 4. Allowing users to download the written file after submission and then setting force download after clicking the download link.  

To implement this, you will need to add the download link code:  

echo 'Click <a href="/phpformtotextfile/textfolder/writethistextfile.txt">here</a> to download this file.<br />';

 

echo '<a href="/phpformtotextfile">Enter another text.</a>';  

And place that after this line:  

echo 'Your text has been written to the file successfully.<br />';  

The above download link, when displayed on the browser, still does not “force download” the text file. You need to use .htaccess to force download the text file inside the textfolder directory.  

To do this:  

1. Open a text editor.

2. Copy and paste the code below:

 

<Files *.txt>

 

ForceType application/octet-stream

 

Header set Content-Disposition attachment

 

</Files>

 

3. Save it as .htaccess

4. Upload the .htaccess to your textfolder directory.  

When the user clicks the download link, instead of opening the text file and displaying it on the browser, it will display the force download dialog:

 

 

To download the file, the user will click the “save” option in the download dialog box.  

You can download the modified and final “PHP form to text file PHP scripthere 

IMPORTANT: Do not forget to change the $filepath to reflect your own exact path.



 
 
>>> 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: