-
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
-
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($fp, 1024);
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);
?>
-
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
-
 Originally Posted by jdvorak
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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|