我有以下 php 文件,它采用一些表单值并写入文本文件并创建一种易于查看的格式:
$filename = "blah.txt"; #Must CHMOD to 666, set folder to 777
$text = "\n" . str_pad($fname, 30) . "" . str_pad($lname, 30) . "" . str_pad($tdate, 20) . "" . str_pad($ydept, 30) . "" . str_pad($percentage, 0) . "%";
$fp = fopen ($filename, "a"); # a = append to the file. w = write to the file (create new if doesn't exist)
if ($fp) {
fwrite ($fp, $text);
fclose ($fp);
#echo ("File written");
}
else {
#echo ("File was not written");
}
已经创建的文本文件具有如下所示的预格式:
@@@@@@ CLINICAL EXAM @@@@@@
First Name Last Name Date Taken Department Percentage
========== ========= ========== ========== ==========
随着新记录的写入,它看起来像这样:
@@@@@@ CLINICAL EXAM @@@@@@
First Name Last Name Date Taken Department Percentage
========== ========= ========== ========== ==========
Lolll Pelol 02/13/13 Nursing 92%
Catger Powhytr 02/13/13 Nursing 92%
随着新用户参加考试,它将继续附加到现有文件中。我想设置身份验证,因此它需要用户名/密码来查看文件并保持格式。我可以使用 include() 设置的身份验证;读取文本文件的php文件顶部的方法。我有以下代码可以读取文件:
<?php
$filename = "blah.txt";
$file = $filename;
$contents = file($file);
$string = implode("<br>", $contents);
echo "<pre>" . $string . "</pre>";
echo "<br></br>";
?>
这将显示以下内容:
@@@@@@ CLINICAL EXAM @@@@@@
First Name Last Name Date Taken Department Percentage
========== ========= ========== ========== ==========
Lilll Pelol 02/13/13 Nursing 92%
Catger Powhytr 02/13/13 Nursing 92%
我喜欢做的是显示它保存的格式,以便用户可以阅读。我怎样才能做到这一点?我正在尝试这样做,因为在网络上保留一个文本文件会使每个人都可以看到它,这是一个安全问题。