PDA

View Full Version : Edit Text file Via FORM???



Anayet
December 10th, 2001, 23:15
Is it possible to create a very simple form that will send data to a text file?

I want to be able to write to a text (txt) file via this form, instead of having to upload it when ever i make changes or instead of having to use online ftp uploaders.

Is it possible to do this via a form, if so what should the form have?

And how do i do it?

Thanks

bigperm
December 11th, 2001, 13:37
You would need more than just a form. You would need to use perl (or PHP or ASP etc.) to get the data from a form and to put that data in a text file.

Anayet
December 11th, 2001, 19:45
Where can i get a Very Simple php or cgi script that will do this, most of the scripts that i have seen are very complicated and used to send lots of files, and i just want to be able to change one file over and over again.


Thanks

trekchick
December 11th, 2001, 19:55
try these.

www.hotscripts.com
cgi-resources.com

trekchick

Anayet
December 11th, 2001, 20:14
Originally posted by trekchick
try these.

www.hotscripts.com
cgi-resources.com

trekchick


Most if not all the scripts on those sites are for uploading lots of files.

Any pointers to one that is very simple, and allows the editing of just one file - which is a simple text file.

Dusty
December 11th, 2001, 20:36
#!/usr/bin/perl

read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
($scrap,$file)=split(/=/,$buffer);
if($file ne ""){
open(FILE,">/path/to/file");
print FILE $file;
close(FILE);
print "Content-type: text/html\n\n";
print "Saved";
}else{
print "Content-type: text/html\n\n";
print "<form action=\"save.pl\" method=\"post\">";
print "<textbox name=\"file\"></textbox>";
print "<input type=\"submit\">";
print "</form>";
}Upload this, name it "script.pl" and CHMOD it 755. Edit "/path/to/file" to reflect the path to the file you want to edit online. Then go to script.pl in your browser to use it. A word of caution, there is no security in this script, so depending on whether this file must remain private or not you might want to edit it a bit.

Anayet
December 11th, 2001, 20:56
Thanks Dusty works nicely, i have one error though..

When i write text it comes out with +&b1 signs like below:

Original Text
this is a test

saved/output text
this+is+a+test&B1


Why is this, and how do i fix it?


Thanks

Dusty
December 11th, 2001, 21:01
Ah yes, I knew I left something out. Add these lines:

$buffer=~tr/+/ /;
$buffer=~s/%(..)/pack("c",hex($1))/ge;

Directly after:

read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});

Anayet
December 11th, 2001, 21:28
Sorry to be so picky :D

the + have been removed, but the &B1 right at the end of everything remains, is it possible to remove this?

Dusty
December 11th, 2001, 21:33
Text from textareas is in binary. You could actually convert it to ASCII, but if you're just editing a simple plain text file it would be easier to just go:

$buffer=~s/&B1//g;

Anayet
December 11th, 2001, 23:03
One final thing

What do i add to the script, so that it can write symbols aswell such as:
£"$%=
etc

At the moment if i write these, they just dissapear:cry2:

Thanks

Dusty
December 12th, 2001, 15:23
You can use all of those, but the "=" would cause some problems in the script I wrote above. Use this one instead:
#!/usr/bin/perl

read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
($scrap,$file)=split(/=/,$buffer);
$file=~tr/+/ /;
$file=~s/%(..)/pack("c",hex($1))/ge;
$file=~s/&B1//g;
if($file ne ""){
open(FILE,">/path/to/file");
print FILE $file;
close(FILE);
print "Content-type: text/html\n\n";
print "Saved";
}else{
print "Content-type: text/html\n\n";
print "<form action=\"save.pl\" method=\"post\">";
print "<textarea name=\"file\"></textarea>";
print "<input type=\"submit\">";
print "</form>";
}

Anayet
December 12th, 2001, 18:51
Thanks for all the help Dusty, everythings working perfectly:)



Thanks again, appreciate it:biggrin2:

Dusty
December 13th, 2001, 14:23
No problem. :)

Anayet
December 15th, 2001, 00:16
I'm just curious about one last thing:D

Is it possible to modify the above script, so that instead of saving the text over the whole file, it just ADDs to the already existing text.

For example, if i submited text which said:
today is today

and then i wanted to add text tomorrow is tomorrow

Would it be possible to modify the script so that the final output would be:


tomorrow is tomorrow

today is today


And anything added after that would be added like the above too.

Is this possible? or am i just talking nonsense:D

Thanks

Dusty
December 15th, 2001, 06:21
Change:

open(FILE,">/path/to/file");

To:

open(FILE,">>/path/to/file");

Anayet
December 15th, 2001, 16:13
Is it possible to make the Newly entered text to come first?

For example, if i entered:

ME

And then used the script again and typed in This Is
Then instead of coming up as:
ME This Is

Can I make the out put be backwards so that it shows:
This is ME


Is this possible?

Dusty
December 15th, 2001, 17:44
Try this:
#!/usr/bin/perl

read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
($scrap,$file)=split(/=/,$buffer);
$file=~tr/+/ /;
$file=~s/%(..)/pack("c",hex($1))/ge;
$file=~s/&B1//g;
if($file ne ""){
open(FILE,"</path/to/file");
@save_file=<FILE>;
close(FILE);
unshift(@save_file,$file);
open(FILE,">/path/to/file");
foreach $line(@save_file){
print FILE $line;
}
close(FILE);
print "Content-type: text/html\n\n";
print "Saved";
}else{
print "Content-type: text/html\n\n";
print "<form action=\"save.pl\" method=\"post\">";
print "<textarea name=\"file\"></textarea>";
print "<input type=\"submit\">";
print "</form>";
}

Anayet
December 15th, 2001, 21:05
As usual, this is working beautifully :)


Thanks for all the help:D

Dusty
December 15th, 2001, 21:40
Glad I could help.