The issue is, I am trying to write to a file on the server as a response to a request initiated by the Client Browser. NOTE: It is a LONG time since I have worked on TypeScript / PHP / Servers so do NOT assume that I know a lot of stuff by default :-)
I have based my code on this example: /questions/11240996/write-javascript-output-to-file-on-server
I ended up with code that uses PHP on the server side to create the file "h.txt" in the https://objective.no/webapptest2/ folder The file h.txt an empty is indeed created, but its content "456" is never written: I get no error messages indicating why.
Below is my code for Client and Server Side
Server Side
//PHP code: (A little elaborate, but it should work)
//h.txt is created, but its content is never written: read only problem?? I can't find how to see if //the file is read only or not)
//The php Code is located on Server
<?php
function getData() {
$myFile = fopen("h.txt", "w");
fclose($myFile);
sleep(5);
//return "link to h.txt";
}
getData();
// Tried to return different values for the URL below:
// "h.txt" is created, but its content "456" is never // written
return "URL to h.txt";
?>
//On the Client side I have the following code:
//The Client is written in Angular, and the function that triggers the data exchange with server
testWriteToServerFile() {
let data: string = "456";// this is your data that you want to pass to the server
//next you would initiate a XMLHTTPRequest as following (could be more advanced):
var url = url to the server side PHP code that will receive the data.
var http = new XMLHttpRequest();
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", data.length.toString());
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);//check if the data was received successfully.
}
}
http.send(data);
}
(I tried to include proper Links to server etc, but the Post was then classified as SPAM?)
Is the problem that h.txt on the Server is Read Only for the Client? What is the solution?
I am expecting to be able to write text to the h.txt file on the server.