search

Home  >  Q&A  >  body text

Issues in converting TypeScript/Angular client-generated server-side text files

<p>"The problem is that I'm trying to write text content to a server-side file via a request initiated by the Client browser. NOTE: This has been quite some time since I've been working on TypeScript/PHP/Server, so Please don't assume I know everything by default :) I adapted it from my own code: /issue/11240996/Output JavaScript to File -server<br /><br /> I ended up with one using PHP Code to create the file "h.txt" on the server side. An empty file "h.txt" is indeed created, but its content "456" is never written: I never receive an error message as to why."456"是文件的内容,</p><p><br /></p> <h2>服务器端</h2> <pre class="brush:php;toolbar:false;">//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"; ?></pre> <pre class="brush:php;toolbar:false;">//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); }</pre> <p>(我试图包括适当的链接到服务器等,但这篇文章被归类为垃圾邮件?)<br /><br />服务器上的h.txt对客户端是只读的问题吗?解决方案是什么?<br /><br />我希望能够将文本写入服务器上的h.txt文件。</p><p><br /></p>
P粉022140576P粉022140576478 days ago484

reply all(1)I'll reply

  • P粉135799949

    P粉1357999492023-08-08 13:53:33

    Okay, here you open a text file for writing and then immediately close it.

    function getData() {
      $myFile = fopen("h.txt", "w");
      fclose($myFile);
      sleep(5);

    What you actually need to do is write the content into a data variable and then pass in the URL as a parameter like this:

    function getData() {
    $myFile = fopen("h.txt", "w");
    fwrite($myFile, filter_input(INPUT_POST, 'data', FILTER_SANITIZE_SPECIAL_CHARS));
    fclose($myFile);

    I also personally prefer input content (input_put_content()) instead of fopen/fclose.

    reply
    0
  • Cancelreply