Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Writing to a text file on the website server

Question posted by: Mikey123 (Newbie) on September 29th, 2006 03:23 AM
I want to be able to write text data to a (preexisting) text file on my website.

have readup heaps, and tinkered around a bit,
and it seems this might actually be possible afterall,
(because of the aweful security risk it poses) at least with javascript.

Can someone clear this up for me, is it possible
(and if so, how) ?

Cheers
Mike
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
acoder's Avatar
acoder
Site Moderator
10,173 Posts
May 15th, 2008
09:58 AM
#2

Re: Writing to a text file on the website server
Not possible with JavaScript alone, but possible with Ajax. Use a server-side script to append to the file and use JavaScript to post to that script.

Reply
pronerd's Avatar
pronerd
Expert
300 Posts
May 15th, 2008
10:15 PM
#3

Re: Writing to a text file on the website server
Quote:
Originally Posted by Mikey123
and it seems this might actually be possible after all

How?



Quote:
Originally Posted by Mikey123
Can someone clear this up for me, is it possible

Acoder, as usual, is correct. JavaScript is a client side language. Meaning it runs on the client, in this case the browser. It can not write to disk on a remote server. It can not even run on the server. HTML and or JavaScript can be used to send a file to the server, but you would need server side code there to receive it and write it to disk for you.

If you know of some way around this we are all ears.

Reply
rnd me's Avatar
rnd me
Expert
204 Posts
May 16th, 2008
03:15 AM
#4

Re: Writing to a text file on the website server
Quote:
Originally Posted by pronerd
How?




Acoder, as usual, is correct. JavaScript is a client side language. Meaning it runs on the client, in this case the browser. It can not write to disk on a remote server. It can not even run on the server. HTML and or JavaScript can be used to send a file to the server, but you would need server side code there to receive it and write it to disk for you.

If you know of some way around this we are all ears.

Code: ( text )
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>javascript-only file writing</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. </head>
  7. <body>
  8. <h1>javascript fileIO demo</h1> 
  9. <b>
  10. javascript can use standard http methods like <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html">GET, PUT, and DELETE</a> to create, modify, and manage files on a remote server. The only configuration needed on the server, is the allowance of public or user-based write permissions.
  11.  
  12. this can be done on window iis server by right-clicking a folder in the IIS web site folder-view.
  13. chmod can be used on unix machines.
  14.  
  15. if you use apache, then you will need a bit of server-side code called a <a href="http://httpd.apache.org/docs/1.3/misc/FAQ.html#putsupport">put handler</a> to catch the PUT requests. (<a href="http://hpwww.ec-lyon.fr/~vincent/apache/mod_put.html">module here</a>)
  16. </b>   
  17. <br />
  18. <hr />
  19. <br />
  20.  
  21. open <a href='newPage.htm' target="_blank">my new page</a> in a new tab/window.
  22. <br />
  23. <br />
  24.  
  25. <textarea rows='10' cols='80' id='userValue'>Hello World</textarea> 
  26. <br />
  27. <br />
  28.  
  29.  
  30. <input type='button' value='Save' onclick="doSave()"  />   
  31.  
  32. &nbsp;&nbsp;&nbsp;
  33. <input type='button'  value='Load' onclick="doLoad()"  />   
  34.  
  35. <script type='text/javascript'>
  36.  
  37.  
  38. function el(tid) {return document.getElementById(tid);}
  39.  
  40. function IO(U, V) {
  41. //LA MOD String Version. A tiny ajax library.  by, DanDavis
  42.     var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
  43.     X.open(V ? 'PUT' : 'GET', U, !1);
  44.     X.setRequestHeader('Content-Type', 'text/html')
  45.     X.send(V ? V : '');
  46. return X.responseText;} 
  47.  
  48.  
  49. function doSave(){
  50.     IO("newPage.htm" , el("userValue").value );
  51. }
  52.  
  53. function doLoad(){
  54.     el("userValue").value = IO("newPage.htm");
  55. }
  56.  
  57. </script>
  58.  
  59.  
  60. </body>
  61. </html>

Reply
acoder's Avatar
acoder
Site Moderator
10,173 Posts
May 16th, 2008
10:02 AM
#5

Re: Writing to a text file on the website server
Interesting, though it could pose a security risk, so should be for trusted users only.

This could be the subject of an article (hint, hint ;))

Reply
Reply
Not the answer you were looking for? Post your question . . .
174,847 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Javascript / DHTML / Ajax Forum Contributors