为什么我的恐惧不起作用?

Why isn't my fread working?

Together 是一个程序,用于查看包含假期的 "csv" 文件,允许某人选择标有数字的按钮,然后将随机选择的假期写入文件。它不工作。有什么帮助吗?具体来说,当我启用 ini_set 设置等时,"choose.php" 不工作并且没有收到任何错误

HTML 文件:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function start(){
    $.ajax({
        url:"cgi-bin/php/holiday/start.php",
        success:function(code){
            $("div#content").html(code);
        }
    }); 
}
function choose(link){
    var data_id = $(link).attr('rel');
    var post = {
        id : data_id
    }
    $.ajax({
        url:"cgi-bin/php/holiday/choose.php",
        data:post,
        method:"POST",
        success:function(code){
            $("div#content").html(code);
        }
    }); 
}
$(document).ready(function(){
    start();
});
</script>
</head>
<body>
<header id='header'></header>
<div id='content'></div>
<div id='footer'></div>
</body>
</html>

start.php

<?php
$row = 1;
if (($handle = fopen("csv/index.csv", "r")) !== FALSE) {
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p>There are {$num} choices left</p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        ?><button onClick='choose(this);' class='choice' rel='<?php echo $c;?>'><?php echo $c;?></div><?php
    }
  }
  fclose($handle);
}?>

choose.php

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$file = fopen("csv/index.csv","r");
$holidays = explode(',',fread($file));
fclose($file);
shuffle($holidays);
$file = fopen("csv/choices.csv","a");
$holiday = $holidays[$_GET['rel']];
fwrite($file, "MY NAME GOT " . {$holiday});
fclose($file);
unset($holidays[$_POST['rel']]);
$file = fopen("csv/index.csv","w");
fwrite($file,implode(",",$holidays);
fclose($file);
echo "YOU GOT {$holiday}";

?>

csv/index.php

christmas,chinese new year

csv/choices为空

fread() 需要第二个参数,即要读取的字节数。如果要读取整个文件,可以使用filesize()函数:

$holidays = explode(',',fread($file, filesize($file)));

但是如果你想读取整个文件,你可以使用file_get_contents()

$holidays = explode(',', file_get_contents("csv/index.csv"));

您可以重写文件:

file_put_contents("csv/index.csv", implode(',', $holidays));