我試過並尋找解決方案,但找不到任何權威。PHP修改文本文件中的單行
基本上,我有一個txt文件,列出用戶名和密碼。我想要能夠更改某個用戶的密碼。
內容user.txt文件,的:
user1,pass1
user2,pass2
user3,pass3
我試過下面的PHP代碼:
// $username = look for this user (no help required)
// $userpwd = new password to be set
$myFile = "./users.txt";
$fh = fopen($myFile,'r+');
while(!feof($fh)) {
$users = explode(',',fgets($fh));
if ($users[0] == $username) {
$users[1]=$userpwd;
fwrite($fh,"$users[0],$users[1]");
}
}
fclose($fh);
您的解決方案的問題是,您正在處理文本(順序)文件作爲二進制文件。你使用的fwrite在這裏不起作用。 –
你的問題是你爲什麼不應該使用平面文本文件而是數據庫的一個例子 – 2012-09-19 05:33:32
另外,還有一個同步問題。所以,你應該嘗試使用['flock'](http://php.net/manual/en/function.flock.php)。或者,它可能容易讓一些用戶感到憤怒。 – Prasanth