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 /选择为空
答案 0 :(得分:1)
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));