1.

Solve : Edit text file trough PHP?

Answer»

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


julleHave 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.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 "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 <<


Edit 'File-name':-


{$read}







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

HOPE this helpsSorry 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.  "" 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 "".



Discussion

No Comment Found