jquery - Update text file in remote computer by web access? -
i have linux machine running web server php. contain text file name 'autorun.conf' - configuration file, see below.
the user can read file ('autorun.conf') local computer - using web browser (e.g. google chrome).
what happen files parse , user can see current configuration, user can update configuration , send linux.
i did parts of code : html file read file linux machine ,parse it,show , let user change it.
for example following code read file 'result' string
function button1_onclick() { console.log("button click"); $.get('/autorun.conf', function(result) {
after change configuration last thing send update 'result' string server.
i did not see how can it. found plenty of file upload plugins enable me send multiple file drag , drop , find simple example 'form' enable me it.
the simplest version of ajax (jquery) upload 1 file
but did not find simple code let me send update string make file in linux.
i glad thank help.
nir
this html file , below autorun.conf file
<!doctype html> <!doctype html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> <script language="javascript" type="text/javascript"> // regular expression // http://regex101.com/ var autorunfile; function button1_onclick() { console.log("button click"); $.get('/autorun.conf', function(result) { console.log(typeof(result)); if (result == 'on') { alert('on'); } else if (result == 'off') { alert('off'); } else { // find specified model autorunfile = result; var pattern = /.*model.*/g; var line = pattern.exec(result); console.log(line[0]); pattern = /\".*\"/g; var value = pattern.exec(line[0]); console.log(value[0]); $("#model").val(value[0]); // find ip address pattern = /.*app_ip.*/g; line = pattern.exec(result); console.log(line[0]); pattern = /\".*\"/g; value = pattern.exec(line[0]); console.log(value[0]); $("#ipaddress").val(value[0]); // find model options // m flag tell treat each end of line ( , end of file) // g tell find matches , not first 1 pattern = /(.* =$)/gm; //line = pattern.exec(result); line = result.match(pattern); /// note match give al matches in contrary exec // remove 2 first entries in array var found = $.inarray('stop_programs', line) > -1; if (found >= 0) { line.splice(found, 2); } // trim / chop ' =' end of string for( i=0 ; i<line.length ; i++) { line[i] = line[i].substring(0, line[i].length -2); } var selectlist = $("#myselect"); (k = 0; k < line.length; k++) selectlist.append("<option value='" + line[k]+ "'>" + line[k] + "</option>"); } } ); } function changemodel() { var val = $( "#myselect option:selected" ).text(); console.log(val); $("#model").val(val); } function button2_onclick() { // extract again 'model' line var pattern = /.*model.*/g; var line = pattern.exec(autorunfile); //console.log(line[0]); var newmodel = "model = " + $("#model").val(); //console.log(newmodel); var autorunfile1 = autorunfile.replace(line[0], newmodel); //alert(autorunfile1); pattern = /.*app_ip.*/g; line = pattern.exec(autorunfile1); var appip = "app_ip =" + $("#ipaddress").val(); console.log(appip); var autorunfile2 = autorunfile1.replace(line[0], appip); // big problem, how can send 'autorunfile2' string linux machine } </script> </head> <body> <p> <table> <tr> <td>model</td> <td><input id="model"></td> <td>ip address</td> <td><input id="ipaddress"></td> </tr> <tr/> <tr> <td/> <td><input id="button1" type="button" onclick="return button1_onclick()" value="get" /></td> <td/> <td><input id="button2" type="button" onclick="return button2_onclick()" value="set" /></td> </tr> <tr> <td/> <td><select id="myselect" onchange="changemodel()" ></select></td> </tr> </table> </p> <div id="mydiv"> </div> </body> </html>
and autorun.conf file
# configuration file autorun model = "nexus_stated_eth"; app_ip = "192.168.15.110"; debug_mode = "false"; work_dir = "/opt/goji"; stop_programs = ( { name = "mbus_listener"; }, { name = "ethernet_listener"; }, { name = "gojimanager"; }, { name = "hosteth_listener"; }, { name = "bossimx28"; } ); # words substitute in program arguments: substitutions = ( { key = "boardid"; value = "1"; }, { key = "plugins_path"; value = "/tmp/1"; }, { key = "technician_socket_port"; value = "9979"; }, { key = "node_id"; value = "10"; } ); nexus_legacy = { message = "starting board nexus legacy"; programs = ( { name = "bossimx28"; args = ("-n", "boardid", "-f", "plugins_path"); }, { name = "gojimanager"; args = ("-n", "boardid", "-f", "plugins_path", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus"); }, { name = "ethernet_listener"; args = ("-n", "technician", "-f", "plugins_path", "-p", "technician_socket_port" , "-i", "eth0"); } ) }; nexus_legacy_cmd = { message = "starting board nexus legacy cmd"; programs = ( { name = "bossimx28"; args = ("-n", "boardid", "-f", "plugins_path"); }, { name = "gojimanager"; args = ("-n", "boardid", "-f", "plugins_path", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus"); } ) }; nexus_stated_mbus = { message = "starting board nexus stated mbus"; programs = ( { name = "bossimx28"; args = ("-n", "boardid", "-f", "plugins_path"); }, { name = "mbus_listener"; args = ("-p", "node_id", "-n", "host", "-f", "plugins_path", "-d", "/dev/ttyfiq0"); }, { name = "gojimanager"; args = ("-n", "boardid", "-f", "plugins_path", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus_stated"); } ) }; nexus_stated_eth = { message = "starting board nexus stated eth"; programs = ( { name = "bossimx28"; args = ("-n", "boardid", "-f", "plugins_path"); }, { name = "gojimanager"; args = ("-n", "boardid", "-f", "plugins_path", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus_stated"); }, { name = "hosteth_listener"; args = ("-n", "host", "-f", "plugins_path", "-p", "technician_socket_port" , "-i", "eth0"); } ) }; nexus_stated_usb_rj45adaptor = { message = "starting board nexus stated usb eth"; programs = ( { name = "bossimx28"; args = ("-n", "boardid", "-f", "plugins_path"); }, { name = "gojimanager"; args = ("-n", "boardid", "-f", "plugins_path", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus_stated"); }, { name = "hosteth_listener"; args = ("-n", "host", "-f", "plugins_path", "-p", "technician_socket_port" , "-i", "eth1"); } ) }; nexus_stated_usb_eth = { message = "starting board nexus stated eth"; programs = ( { name = "bossimx28"; args = ("-n", "boardid", "-f", "plugins_path"); }, { name = "gojimanager"; args = ("-n", "boardid", "-f", "plugins_path", "-g", "98", "-s", "6000000", "-d", "/dev/spidev1.0", "-p", "nexus_stated"); }, { name = "hosteth_listener"; args = ("-n", "host", "-f", "plugins_path", "-p", "technician_socket_port" , "-i", "usb0"); } ) };
finally, help, found solution
i show 2 different php solutions ( ajax little different)
the first solution in html file add
// big problem, how can send 'autorunfile2' string linux machine jquery.ajax({ type: "post", // http request method. url: "uploadstring.php", // url of data fetch. data: autorunfile2, // don't add data url. datatype: "text", // execute response script once it. success: function() // call function when done. { alert("success"); } });
this take string , send http post command
and in server side following php file take string , save file
<?php file_put_contents('autorun.conf1', file_get_contents('php://input')); ?>
in second solution ( @ 'data' filed)
// big problem, how can send 'autorunfile2' string linux machine jquery.ajax({ type: "post", // http request method. url: "uploadstring.php", // url of data fetch. //data: autorunfile2, // don't add data url. data:{post: autorunfile2}, datatype: "text", // execute response script once it. success: function() // call function when done. { alert("success"); } });
and php in server is
<?php $fp = fopen('autorun.conf1', 'w'); $abc = $_post['post']; fwrite($fp, $abc); ?>
thanks
Comments
Post a Comment