• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Edit Text file Via FORM???

Anayet

New Member
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
 
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.
 
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
 
Code:
#!/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.
 
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
 
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'});
 
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?
 
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;
 
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
 
Last edited:
You can use all of those, but the "=" would cause some problems in the script I wrote above. Use this one instead:
Code:
#!/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>";
}
 
Thanks for all the help Dusty, everythings working perfectly:)



Thanks again, appreciate it:biggrin2:
 
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
 
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?
 
Try this:
Code:
#!/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>";
}
 
Back
Top