DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Hybrid View

  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

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links