dongshao2967 2017-03-24 09:14
浏览 32

如何更改文本文件并在php中读取这些更改

I want to read in a message from a JS file, and then change a global variable in that js file so that it will adjust its performance (and then empty the text file). Unfortunately, it always returns the original value of the text file instead of the updated value, and I can't use the updated value until I refresh the page. Is there a better way to do this?

<?php 
switch($_SERVER['REQUEST_METHOD'])
{
  case 'GET':
    if(file_exists('chat.txt') && filesize('chat.txt') > 0){
      $filesize = filesize('chat.txt');
      $f = fopen('chat.txt', "a+");
      $line = fread($f, $filesize);
      $line = htmlspecialchars(str_replace("
", "", $line));
      echo "<script> post = '" . $line  . "';</script>";
      fclose($f);
      fclose(fopen('chat.txt', 'w'));

    }



    break;  

  case 'POST':
    $function = $_POST['function'];
    switch ($function) {
      case('send'):
        $message = strip_tags($_POST['message']);
        $f = fopen('chat.txt', 'w');
        fwrite($f, $message);
        fclose($f);
        break;
    }
}

?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行