writing and reading a string to and from a server file


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: writing and reading a string to and from a server file

  1. #1
    Join Date
    Mar 2010
    Posts
    2

    writing and reading a string to and from a server file

    I have a web page with several JavaScript functions. One of the things I need to do is to read a string from a server side file (say tmp.txt) into a Javascript variable and also to write the value of a Javascript variable to the file, overwriting previous contents. That is all I want to do.

    How can I do that? Thanks

    Joe

  2. #2
    Join Date
    Feb 2010
    Posts
    17
    Joe, at the risk of sounding patronizing, JavaScript cannot read or write information to or from files (on the server OR on a clientís computer.) Youíll need server side scripting for that: PHP, ASP.NET, Classic ASP and many others. I can give you code for PHP, but I do not know any other server languages.

    PHP Code:
    <?php
    $filename 
    "tmp.txt";

    //reading from file
    $fp fopen($filename"r") or die("Couldn't open $filename Ln 5.");
    while(!
    feof($fp)) {
        
    $line fgets($fp1024);
        echo 
    $line;
    }
    fclose($fp);

    //writing to file
    $fp fopen($filename"w") or die("Couldn't open $filename Ln 13.");
    fwrite($fp"Hello World");
    fclose($fp);
    ?>

  3. #3
    Join Date
    Mar 2010
    Posts
    2

    RE: writing and reading a string to and from a server file

    Steve,

    Thanks for the explanation and script. I thought this was the case but wasn't sure. The script is a big help since I am just starting to learn PHP. Thanks again.

    Joe

  4. #4
    Join Date
    Apr 2010
    Posts
    1
    Quote Originally Posted by jdvorak View Post
    Steve,

    Thanks for the explanation and script. I thought this was the case but wasn't sure. The script is a big help since I am just starting to learn PHP. Thanks again.

    Joe
    why not sure? i think it 's exact !

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center