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 script” here. IMPORTANT: Do not forget to change the $filepath to reflect your own exact path.
blog comments powered by Disqus |
|
|
|
|
|
|
|