Jump to content
xisto Community
Sign in to follow this  
matak

[php]simple Flat File Text Manipulator Example on how to use forms to write to files in PHP

Recommended Posts

I made a simple flat file text editor, that can show you probably how simple it is to use forms with php and write that data to file.
This example has 2 files, submit.php, and postit.html.
Submit.php is used to write title, and some text, and add html tags, and paragraph tags where new paragraphs are.

Here's the file with comments. I think that HTML really doesn't need some more explaining.

Title: <br /> <input type="text" name="title" size="53"> <br /> Text: <br /> <textarea name="text" rows="20" cols="40"></textarea><br /> <br /> <input type="submit" value="Save"> </form> <?php  if (@$_POST['title']=="" || @$_POST['text']==""){	 echo "Enter text to save!"; } else {	 //this is path to file	 $filename = './postit.html';	 //i used $title to strip HTML/PHP tags < >, and added <h2> HTML tag to decorate title	 $title = "<h2>".strip_tags($_POST['title'])."</h2>\n";	 //with this $_POST['text'] is stripped from html/php tags like title, but also preg_replace 	 //is used to replace \n(every enter that u hit when writing tekst),	 //and instead of that added <p> tags which decorate your new paragraph	 $tekstmanage = preg_replace('/\n/','</p><p>', strip_tags($_POST['text']));	 	 //i should probably write a whole preg_replace function instead, which i will probably later, 	 //but this $tekst variable is used to add finishing and closing tags	 //to our tekst manage. you can see how preg_replace adds first closing than starting tags	 //and with this we just simply add starting before closing and closing after starting tags :confused:;)	 $tekst = "<p>".$tekstmanage."</p>\n";	 	 //after our variables are defined, we combine them in unique variable which we'll write to file	 $content = $title.$tekst;	 	 //This below is basic write to file function that i found on php.net, and rearanged	 //i'll just explain it briefly	 //	 //this checks weather our filename is writeable (on unix systems like here on Xisto)	 //you need to change permissions to the file that allows writing to world 646	 if (is_writeable($filename)){	 		 //if file cant be opened to add -> 'a' <- in the fopen construct		 //just echo sorry		 if(!$handle= fopen($filename,'a')){			 echo "Sorry, you can't write to file1";		 }		 //if file cant be written to, same		 if (fwrite($handle, $content) === FALSE){			 echo "Sorry, you can't write to file2";			 exit;		 }	 //if both of those terms are fine just echo the confirmation text	 echo "You wrote to file ".$filename."<br />";	 echo "<a href=\"postit.html\">View</a>";	 	 }	 //if file isn't writeable	 else {	 echo "Sorry, you can't write to file3";	 } }  ?>

Well, there it is, hope u have fun with it.

Also this needs a bit more work to be done with it, so i won't post dl link. If you have any questions about the script, you know what to do :)

EDIT: Note to moderators, sorry this is not a real text EDITOR (it only writes), so can u please edit the title, to suit the script better.. Thanks :)
Edited by jlhaslip (see edit history)

Share this post


Link to post
Share on other sites

Greetingsman thanx for the script
it simple and short.

but what is the extention of the file when you save it ???

..........


file extension, and path is given with this variable

$filename = './postit.html';

./ means to look in the current directory, and filename's extension is .html. You can change extension to any one you like just my renaming the file that PHP looks for :). This script doesn't create file if it doesn't egzist, it just writes to egzisting file, so make sure to create file you will be writing to before running script.

Yes, it's short, but i posted it mostly for that preg_replace thing, although i admit it needs some more work, so that it writes nicer code than it does now.

And soon i'll create a bit more powerfull, but again, simple, text editor (script that reads files, and allows you to edit them), but that needs a bit more work, and probably use of file_get_contents function. I'll look into it when i have more time :)

EDIT: Thank you jlhaslip for changing the title, manipulator, sounds nice :(
Edited by matak (see edit history)

Share this post


Link to post
Share on other sites

file extension, and path is given with this variable

$filename = './postit.html';

./ means to look in the current directory, and filename's extension is .html. You can change extension to any one you like just my renaming the file that PHP looks for :). This script doesn't create file if it doesn't egzist, it just writes to egzisting file, so make sure to create file you will be writing to before running script.

Yes, it's short, but i posted it mostly for that preg_replace thing, although i admit it needs some more work, so that it writes nicer code than it does now.

And soon i'll create a bit more powerfull, but again, simple, text editor (script that reads files, and allows you to edit them), but that needs a bit more work, and probably use of file_get_contents function. I'll look into it when i have more time :)

EDIT: Thank you jlhaslip for changing the title, manipulator, sounds nice :(

Greetings

Thanx man for the quick repply

i'm looking forward to use this script and see it in action :)

thanx, have a nice day :(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.