douquanhui5735 2016-08-11 11:16
浏览 32
已采纳

PHP filesize if-statement即使在false时仍然执行

<?php
$seatsArray = array();
$myFile = fopen("seats.txt", "w") or die("Unable to Open File!");
if(filesize("seats.txt") == 0) {
    for($x = 0; $x < 10; $x++) {
        fwrite($myFile, "0
");
    }
}   
$seatsArray = file("seats.txt", FILE_IGNORE_NEW_LINES);
fclose($myFile);
?>

var array = [<?php echo '"'.implode('","', $seatsArray ).'"' ?>];

This PHP code is at the top of my script section in head. The seats.txt file is full of zeroes initially to represent vacant seats on a flight and through other functions, the seats will fill up (represented by 1s). I can get the 1s to write to the file but as soon as I reload the page, the if-statement seems to execute regardless of its condition being false and resets everything back to zero.

  • 写回答

2条回答 默认 最新

  • dongren9739 2016-08-11 11:25
    关注

    The reason is due to this w mode

    w- (Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist)
    

    So every time your file gets blank

    Use a or a+ if you want to append at the right of file or r+ if you want to right from starting

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据