附加到php文件而不是txt文件

Si8*_*Si8 0 php file-io file

我有以下脚本从表单中获取一些值,并在每次在表单中输入新值时附加到文本文件:

$filename = "nic.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");
}
Run Code Online (Sandbox Code Playgroud)

问题是,我不是要写入一个在存储数据方面不安全的txt文件,而是如何让我们说附加到php文件,这样用户在查看网络上的文件之前需要进行身份验证?我想要某种身份验证(密码/用户名),所以不是每个人都能看到它.并且使用txt文件我认为不可能.

我的SQL写入数据文件,我注释掉,直到找到最佳选项为:

// Write to DB
//$conn = new mysqli('host', 'user', 'pass', 'db');

// check connection
//if (mysqli_connect_errno()) {
//  exit('Connect failed: '. mysqli_connect_error());
//}

// store the values in an Array, escaping special characters for use in the SQL statement
//$adds['fname'] = $conn->real_escape_string($fname);
//$adds['lname'] = $conn->real_escape_string($lname);
//$adds['tdate'] = $conn->real_escape_string($tdate);
//$adds['ydept'] = $conn->real_escape_string($ydept);
//$adds['percentage'] = $conn->real_escape_string($percentage);

// sql query for INSERT INTO users
//$sql = "INSERT INTO keepScore ('fname', 'lname', 'tdate', 'ydept', 'percentage' ) VALUES ('". $adds['fname']. "', '". $adds['lname']. "', '". $adds['tdate']. "', '". $adds['ydept']. "', '". $adds['percentage']. "')"; 

// Performs the $sql query on the server to insert the values
//if ($conn->query($sql) === TRUE) {
//  echo 'users entry saved successfully';
//}
//else {
//  echo 'Error: '. $conn->error;
//}

//$conn->close();
Run Code Online (Sandbox Code Playgroud)

Bra*_*rad 6

将文本文件保留在Web服务器的文档根目录之外,并在读取文件时使用PHP脚本提供身份验证/授权.

看看readfile().