简体   繁体   English

javascript 内部的 PHP fopen() 逻辑错误

[英]PHP fopen() inside javascript logic bug

I want to create a text file stole to localhost directory.我想创建一个文本文件窃取到 localhost 目录。 below is my code.下面是我的代码。

below app.php下面app.php

            document.getElementById("save_button").addEventListener("click", function() {
          
              var content =  document.getElementById("final_span").value();

              var file_name =document.getElementById("filename").value();
              <?php
              $fn = strstr($file_name,'.', true);

              $dir = "../project/Record";

              $file = fopen($dir."/".$fn.".txt","w+");

              fwrite($file, $content);

              fclose($file);
              ?>
            });
      </script>```

Your js will execute in browser and php is server side language.您的 js 将在浏览器中执行,php 是服务器端语言。 You can't control php within js as you've done.您无法像您所做的那样在 js 中控制 php。 You can do it by ajax call from you js to php file and create a file.您可以通过 ajax 从您的 js 调用 php 文件并创建一个文件来完成。

    <script>  document.getElementById("save_button").addEventListener("click", function() {
              
                  var content =  document.getElementById("final_span").value;
    
                  var file_name =document.getElementById("filename").value;
    
    var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
          if (this.readyState == 4 && this.status == 200) {
            document.getElementById("txtHint").innerHTML = this.responseText;
          }
        };
        xmlhttp.open("GET", "app.php?file_name=" + file_name, true);
        xmlhttp.send();
    
    </script>

app.php app.php

<?php $file_name= $_GET['file_name'];
              $fn = strstr($file_name,'.', true);

              $dir = "../project/Record";

              $file = fopen($dir."/".$fn.".txt","w+");

              fwrite($file, $content);

              fclose($file);
              ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM