Page 1 of 1

php notepad

Posted: Sat Aug 25, 2007 9:17 pm
by ralphi91
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


i would like to make a notepad to save and load things from a text file stored on my webserver. I would like to have 3 buttons, "save", "load" and "refresh".

I am not good with php and i have found the following parts of code that would probably be useful in creating this.

Code: Select all

<?php
$myFile = "71637213418#$%.txt";

$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$text;
fwrite($fh, $stringData);
$stringData = "----";
fwrite($fh, $stringData);
fclose($fh);//sending

php?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Aug 25, 2007 10:49 pm
by s.dot
What's your question?

small notepad code

Posted: Sat Aug 25, 2007 11:08 pm
by yacahuma
first you need to create a web page with a textarea an the 3 buttons.

Actually the load is not really necessary since you can read it the first time your page is up.

Code: Select all

<?
$path  = '/mydatafolder/datafile.txt';
if (isset($_POST['btn_save']))
{
   $data = $_POST['mydata'];
   file_put_contents ( $path, $data);  
}else{
   $data = implode('', file($path));
}
?>
<html>
<body>
<form action='thisfile.php' method='post'>
<textarea rows="2" cols="20" id='mydata' name='mydata'><?=$data?></textarea>
<input type='submit' name='btn_save' value='Save' />
<input type='submit' name='btn_refresh' value='Refresh' />

<body>
</html>
I may not have all the syntax correct.

Re: small notepad code

Posted: Sat Aug 25, 2007 11:53 pm
by s.dot
Some tag issues here :P
yacahuma wrote:

Code: Select all

<?
.....
Always use <?php to start your PHP code, since it can be used independently of the php.ini short tags setting.
ralphi91 wrote:

Code: Select all

...
php?>
To close a php section, just use ?>. The way you have it would probably cause some parsing errors.

As far as the original post, if your question is how to do it..

Get the contents of a text file

Code: Select all

$textFile = file_get_contents('txtfile.txt');
Display them in a textarea

Code: Select all

echo '<textarea rows="5" cols="30">' . $textFile . '</textarea>';
To change the contents,
Submit the form, then use file_put_contents() (php5+ i believe) or fopen() the file, fwrite() the file, fclose() the file.

Posted: Sun Aug 26, 2007 7:09 am
by kaszu
Don't forget to escape data

Code: Select all

$textFile = file_get_contents('txtfile.txt');
$textFile = htmlentities($textFile);
echo '<textarea rows="5" cols="30">' . $textFile . '</textarea>';