Reputation: 1052
I need to be able to read, display, modify and save the contents of an existing .txt file which is located on a Windows server. I can do the 'read' and 'display' bits already, but not the modify and save back the file.
For example :
Text file is located here : www.myserver.com/files/textfile.txt Content of file is : one_whole_line_like_this
I could of course do it via an FTP client, but I need something more elegant and easier for the end user, so I want the user to be able to access a (password protected) webpage and have it display the current content of textfile.txt (I can do up to here), then have a text box to enter the new content and a 'submit' button. The submit button then writes the new content of the text box to the file, keeping its original name (textfile.txt).
At the moment I'm stuck with thinking which is the best way to do it, never mind implementing it.
I would really appreciate suggestions on the easiest and most elegant way (for the user) to do this. Some example code would also be greatly appreciated.
Upvotes: 1
Views: 3880
Reputation: 4357
I'm assuming in this answer that you have a function web server. Otherwise, I suggest installing Apache. There's plenty of instructions on the web if you need help setting it up. Natively, Apache can create password protected web pages. But, you'll need an SSL extension to actual encrypt information. You will also need PHP, which you will use to save new files on the server. You can get all this as an all-in-one package, like WAMP, as well.
If or when you have a functioning web server, you'd be using a mixture of HTML, CSS, Javascript and PHP for an actual web page.
The general id is you have a client using the webpage. On the client's webpage, you will have a script that sends the data back to your server. On the server, you'll have another script that receives the data and then stores it into the file you want.
First, you'll want to design the page that the client will be visiting. You do that with HTML and CSS. You'd use HTML to add a text box and other things and CSS to change the way it looks on the page.
An HTML example:
<html>
<body>
<textArea>My first html page!</textArea>
<button>Submit</button>
</body>
</html>
A CSS example:
body { background-color: black; }
textArea { width:50%; }
Then you'd collect the information in the text box with Javascript on the browser. Then, you'd send that request to a PHP script on the server. In the PHP script you save the data to the file request. Here's a rough example that uses Javascript and Jquery - Javascript extension:
<script>
/*
The $( "#submitButton" ) is executed every time the user clicks the div that is
associated with it.
The text in the textbox is returned from $( "#submitBox" ).text( );
in $.post, here's what's happening
$.post( ( script name ), ( data to be sent to script ) )
So when your executing $.post( "php_file.php", new_text:$("#submitBox").text() )
You're starting a script called php_file.php and sending the text in text box to a
variable called new_text.
*/
$( "#submitButton ).click( function( ){
$.post( "php_file.php", new_text : $( "#submitBox" ).text( ) );
});
</script>
PHP Script
<?php
file_put_contents( "filename.txt", $_POST[ "new_text" ] );
?>
As these are just snippets of code, I would suggest reading up on HTML, CSS, Javascript, and PHP. You'll find a lot of information on W3schools ( among other sites ). I suggest focusing on HTML first, by playing with it on your home computer ( ie create a .html text file on your computer and open it on a browser ). And if you have problems along the way, this is a great site to post your questions on if all else fails.
Upvotes: 2