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

Code discussion - 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

The actual file writing procedures do not necessarily call the fwrite function right away. Instead, the file path should be declared first: 

$filepath='/opt/lampp/htdocs/phpformtotextfilebasic/textfolder/
writethistextfile.txt';

This should be relative to the root directory. or else the error “Could not open file!” will be displayed. This is also the output of: 

<?php

echo $_SERVER['SCRIPT_FILENAME'];

?> 

If you do not know the full relative path with respect to the root, create a PHP file, name it path.php and upload it to the directory where you need to determine the path. After that, view path.php in the web browser and there will be revealed the full path relative to the root.

In Linux web hosting, it might look like this: /home/www/php-developer.org/formtotextproject/textfolder/writethistextfile.txt 

If you use (not a full path):  $filepath='/phpformtotextfilebasic/textfolder/writethistextfile.txt';  

It will display an error that says fopen could not open the file. Also, be careful with the spelling of the path name, as any error wil also lead to the “Could not open the file” error. 

Now that the path has been set, you are ready to open a file using a file handler:

$filehandler= fopen($filepath, 'w') or die('ERROR: Could not open file!');  

Since this is a file writing tasks, 'w' is used to signify the “writing” mode. By default, if writethistextfile.txt is not yet created, fopen will create and prepare this file for writing.

Finally, now that the file has been opened and is ready for writing, the actual text contents from the web form will be written to the file using the command:

fwrite($filehandler,$textinputs) or die('ERROR: Could not write to file');  

The file handler can be closed after the writing tasks:

fclose($filehandler);  

You can download the complete script discussed above here

If you would like to test this, you can enter any text you like and then click the  “Write this text to a text file” button. 

Go to the textfolder directory and open the file; you should see the text you have just submitted.

Sample screen shot:



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