Akshit Sharma Akshit Sharma - 1 year ago 72
jQuery Question

Writiing to a SERVER file using JS

Tried all solutions.. none of them seem to work for me.

Basically, I have this website UPLOADED on the server that has a button which when clicked does a simple task of loading a file from the server, displaying it and adding +1 to it and then saving it back.

The main question now arises, I tried all the ways which I could find on the net to save the file back but it doesn't seem to work for me. Any specific advice on how can I fix this issue using JS?

UPDATE - One way I am using is - (Doesn't work tho)

fso = new ActiveXObject("Scripting.FileSystemObject");
var s = fso.CreateTextFile("TOTAL.TXT", true);
var text = test1;

Where test1 is the name of the variable which i am trying to write.

Answer Source

assuming that you have php on server try this js

var data = new FormData();
data.append("data" , 1);
var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open( 'post', '/path/to/php', true );

php script

$data = $_POST['data'];
$fname = "filname.txt"

$file = fopen("upload_or_whatever_path/" .$fname, 'w') or die("Unable to open file!");// open file
fwrite($file, $data);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download