Writing to a text file on the website server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mikey123
    New Member
    • Sep 2006
    • 1

    #1

    Writing to a text file on the website server

    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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    • pronerd
      Recognized Expert Contributor
      • Nov 2006
      • 392

      #3
      Originally posted by Mikey123
      and it seems this might actually be possible after all
      How?



      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.

      Comment

      • rnd me
        Recognized Expert Contributor
        • Jun 2007
        • 427

        #4
        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.
        [HTML]


        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <title>javascri pt-only file writing</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        </head>
        <body>
        <h1>javascrip t fileIO demo</h1>
        <b>
        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.

        this can be done on window iis server by right-clicking a folder in the IIS web site folder-view.
        chmod can be used on unix machines.

        if you use apache, then you will need a bit of server-side code called a <a href="http://httpd.apache.or g/docs/1.3/misc/FAQ.html#putsup port">put handler</a> to catch the PUT requests. (<a href="http://hpwww.ec-lyon.fr/~vincent/apache/mod_put.html">m odule here</a>)
        </b>
        <br />
        <hr />
        <br />

        open <a href='newPage.h tm' target="_blank" >my new page</a> in a new tab/window.
        <br />
        <br />

        <textarea rows='10' cols='80' id='userValue'> Hello World</textarea>
        <br />
        <br />


        <input type='button' value='Save' onclick="doSave ()" />

        &nbsp;&nbsp;&nb sp;
        <input type='button' value='Load' onclick="doLoad ()" />

        <script type='text/javascript'>


        function el(tid) {return document.getEle mentById(tid);}

        function IO(U, V) {
        //LA MOD String Version. A tiny ajax library. by, DanDavis
        var X = !window.XMLHttp Request ? new ActiveXObject(' Microsoft.XMLHT TP') : new XMLHttpRequest( );
        X.open(V ? 'PUT' : 'GET', U, !1);
        X.setRequestHea der('Content-Type', 'text/html')
        X.send(V ? V : '');
        return X.responseText; }


        function doSave(){
        IO("newPage.htm " , el("userValue") .value );
        }

        function doLoad(){
        el("userValue") .value = IO("newPage.htm ");
        }

        </script>


        </body>
        </html>[/HTML]

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          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 ;))

          Comment

          Working...