Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> [php]simple Flat File Text Manipulator, Example on how to use forms to write to files in PHP
matak
post May 25 2007, 06:04 AM
Post #1


Super Member
*********

Group: Members
Posts: 413
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



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.

CODE
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 trap17)
     //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 rolleyes.gif

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 smile.gif

This post has been edited by jlhaslip: May 25 2007, 11:57 AM
Go to the top of the page
 
+Quote Post
Blessed
post May 25 2007, 09:05 AM
Post #2


Advanced Member
*******

Group: Members
Posts: 144
Joined: 22-March 07
Member No.: 40,472



Greetings
man thanx for the script
it simple and short.

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

..........
Go to the top of the page
 
+Quote Post
matak
post May 25 2007, 12:02 PM
Post #3


Super Member
*********

Group: Members
Posts: 413
Joined: 4-October 06
From: Psychedelic Realms
Member No.: 31,079



QUOTE(Blessed @ May 25 2007, 11:05 AM) *
Greetings
man 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

CODE
$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 smile.gif. 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 smile.gif

EDIT: Thank you jlhaslip for changing the title, manipulator, sounds nice biggrin.gif

This post has been edited by matak: May 25 2007, 12:05 PM
Go to the top of the page
 
+Quote Post
Blessed
post May 25 2007, 12:25 PM
Post #4


Advanced Member
*******

Group: Members
Posts: 144
Joined: 22-March 07
Member No.: 40,472



QUOTE(matak @ May 25 2007, 12:02 PM) *
file extension, and path is given with this variable

CODE
$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 smile.gif. 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 smile.gif

EDIT: Thank you jlhaslip for changing the title, manipulator, sounds nice biggrin.gif


Greetings

Thanx man for the quick repply

i'm looking forward to use this script and see it in action biggrin.gif

thanx, have a nice day laugh.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Getting List Of Directories And Files Using Php(6)
  2. Change Permission With Php Code(3)
  3. Script: Php Jukebox(4)
  4. I Need Help With File Edit In Php(6)
  5. Include File.php?id=something(13)
  6. Dynamic Image / Signature Generator(12)
  7. Help Needed With Directory/file Listing Code Infinite Loop(5)
  8. Writing To Files And Such(3)
  9. Help Php: How To Load String From Text File (solved)(9)
  10. Php Help Needed Including File In A Page.(2)
  11. Forms, Text Files, And Php For A Signature Generator.(1)
  12. Reading Files From Directory To Array, And Using $_get To Get Them(2)
  13. Edit .txt File In Ftp Via Webpage(2)
  14. <?php ?> Unique Visitors Script(2)
  15. Security Issue Writing Files(1)
  1. Grabt Access To My Protected Files(2)
  2. File Upload(1)
  3. No File Extension(3)
  4. Php And Flash Image Gallery(5)
  5. Php Text Editor(1)
  6. I Just Wrote A Script For A Php Text Editor!(8)
  7. File Checker-how To Check File Whith Html Through Html?(2)
  8. Php & Forms Question(2)
  9. Restrictions On Php & Forms?(2)
  10. Download Script For Mp3 Files(0)
  11. Updating Php File Through A Web Form(5)
  12. Need Some Help In File Browser(8)
  13. Php Configuration File(16)


 



- Lo-Fi Version Time is now: 5th September 2008 - 01:05 PM