Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Edit text file trough PHP  (Read 6312 times)

0 Members and 1 Guest are viewing this topic.

julle

  • Guest
Edit text file trough PHP
« on: May 07, 2006, 12:22:03 PM »
Hellow,

Does anybody knows how to edit a text file trought php. It should be something like this: a text box where you can see the things that are written in the text file (*.txt) and you can edit it. A bit like the moderators of this forum can do. Edit a written text in a text box. There shouldn't be special things like Bold etc... just a box where you can edit a textfilfe and ofcourse a save button :)


can anybody helps me


P.S. I'm running php 4

thanks


julle

Rob Pomeroy



    Prodigy

  • Systems Architect
  • Thanked: 124
    • Me
  • Experience: Expert
  • OS: Other
Re: Edit text file trough PHP
« Reply #1 on: May 08, 2006, 02:31:44 AM »
Have a browse through the >File System< section of the PHP manual.  Pay particular attention to file_get_contents.

If you still can't work it out, come back again.
Only able to visit the forums sporadically, sorry.

Geek & Dummy - honest news, reviews and howtos

wolve

  • Guest
Re: Edit text file trough PHP
« Reply #2 on: May 11, 2006, 06:16:49 PM »
something along these lines should do the trick:

if($_REQUEST['submit'])

{

        $post_data = $_SERVER["DOCUMENT_ROOT"]."root to text file here";

          $data = fopen($post_data, "w");

          $post = stripslashes($_REQUEST['post']);

          fwrite($data, "$post");

        fclose($data);

        print "<font colour=red>File edited successfully[/url]";

}



          $post_data = $_SERVER["DOCUMENT_ROOT"]."root to text file here";

          $data = fopen($post_data, "r");

              $read = fread($data, filesize($post_data) );

          fclose($data);

print <<<EOF

<form method=POST>

<h1>Edit 'File-name':-</H1>


<textarea name="post" cols="35" rows="15">{$read}</textarea>

<input type=submit value=Submit name=submit>

</form>



EOF;



A very old script i used a while ago. Theres simplier ways of doing it, but this one's pretty secure.

Then, to have the .txt file show up on your website, simply use <? include("filename.txt") ?>

Hope this helps

Rob Pomeroy



    Prodigy

  • Systems Architect
  • Thanked: 124
    • Me
  • Experience: Expert
  • OS: Other
Re: Edit text file trough PHP
« Reply #3 on: May 12, 2006, 05:30:34 AM »
Sorry to be picky, but that script involves some practices that are best avoided, I think:

Quote
if($_REQUEST['submit'])
I've only recently discovered how that doesn't work.  The handling of "submit" buttons varies between browsers and is not guaranteed.  If the submit button is the default action on the form, and rather than press the button, the end user hits "enter", the form is submitted without the button being clicked.  Many browsers will ONLY send the submit/value pair, if the button has been clicked.  Therefore, to test whether a form has been submitted, it is much better to include a hidden form element that contains a name/value pair that you check for.  The hidden element will always be sent with the GET/POST variables.

Quote
$post_data = $_SERVER["DOCUMENT_ROOT"]."root to text file here";
DOCUMENT_ROOT is not very portable, since its handling seems to varying according to the O/S PHP is installed on.

Quote
         $data = fopen($post_data, "r");

              $read = fread($data, filesize($post_data) );

          fclose($data);

print <<<EOF

Very bad indeed - no checking for special characters, no parsing of HTML, etc.  Could end up with a very screwed up edit page.

Quote
<textarea name="post" cols="35" rows="15">{$read}</textarea>

The "{}" syntax can be specific to a templating system.  "<?php echo $read; ?>" is recommended.

Quote
Then, to have the .txt file show up on your website, simply use <? include("filename.txt") ?>
The "<? ?>" short form is not guaranteed, and depends on php.ini settings.  For maximum portability, you should always use "<?php ?>".

Only able to visit the forums sporadically, sorry.

Geek & Dummy - honest news, reviews and howtos