2012-09-19 23 views
6

我試過並尋找解決方案,但找不到任何權威。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);  
+1

您的解決方案的問題是,您正在處理文本(順序)文件作爲二進制文件。你使用的fwrite在這裏不起作用。 –

+8

你的問題是你爲什麼不應該使用平面文本文件而是數據庫的一個例子 – 2012-09-19 05:33:32

+0

另外,還有一個同步問題。所以,你應該嘗試使用['flock'](http://php.net/manual/en/function.flock.php)。或者,它可能容易讓一些用戶感到憤怒。 – Prasanth

回答

8

這應該可行! :)

$file = "./users.txt"; 
$fh = fopen($file,'r+'); 

// string to put username and passwords 
$users = ''; 

while(!feof($fh)) { 

    $user = explode(',',fgets($fh)); 

    // take-off old "\r\n" 
    $username = trim($user[0]); 
    $password = trim($user[1]); 

    // check for empty indexes 
    if (!empty($username) AND !empty($password)) { 
     if ($username == 'mahdi') { 
      $password = 'okay'; 
     } 

     $users .= $username . ',' . $password; 
     $users .= "\r\n"; 
    } 
} 

// using file_put_contents() instead of fwrite() 
file_put_contents('./users.txt', $users); 

fclose($fh); 
+0

美麗的一段代碼! – antikbd

2

我認爲當你在使用的preg_replace爲特定之後獲得該文件的file_get_contents使用用戶名

我已經在過去這樣做過一些像這裏的東西

   $str = ""; 

     $reorder_file = FILE_PATH; 
     $filecheck = isFileExists($reorder_file); 

     if($filecheck != "") 
     { 
      $reorder_file = $filecheck; 
     } 
     else 
     { 
      errorLog("$reorder_file :".FILE_NOT_FOUND); 
      $error = true; 
      $reorder_file = ""; 
     } 
     if($reorder_file!= "") 
     { 
      $wishlistbuttonhtml="YOUR PASSWORD WHICH YOU WANT TO REPLACE" 
      $somecontent = $wishlistbuttonhtml; 
      $Handle = fopen($reorder_file, 'c+'); 
      $bodytag = file_get_contents($reorder_file); 
      $str=$bodytag; 
      $pattern = '/(YOUR_REGEX_WILL_GO_HERE_FOR_REPLACING_PWD)/i'; 
      $replacement = $somecontent; 
      $content = preg_replace($pattern, $replacement, $str,-1, $count); 
      fwrite($Handle, $content); 
      fclose($Handle); 
     } 

希望這有助於......

1

這樣做的正確方法是使用數據庫來代替。數據庫可以很容易地進行隨機訪問,用文本文件做得更少。

如果無論出於何種原因無法切換到數據庫,並且系統的用戶數不會超過大約一千人,那麼僅僅讀取整個文件就會簡單多了,轉換它轉換爲PHP數據結構,進行所需的更改,將其轉換回文本並覆蓋原始文件。

在這種情況下,這將意味着file()將文本文件加載到數組中,每個元素都是用戶名和密碼作爲字符串,以逗號分隔數組中的所有元素以分別獲取用戶名和密碼,進行所需的更改,然後將修改後的數據寫入光盤。

您可能還會發現fgetcsv()對於讀取數據很有用。如果你使用SplFileObject並且有最新版本的PHP,那麼fputcsv()也可以用來寫回數據。

但是,僅僅使用數據庫是一個更好的解決方案。這項工作的正確工具。

0

如果您使用的是* nix系統,則可以使用sed;我發現它比整潔與文件播放處理等功能:

exec("sed -i '/^$username,.\+\$/$username,$userpwd/g' ./users.txt 2>&1", $output, $return); 

如果沒有我與GordonM同意和文件解析爲PHP數據結構,操縱它,然後把它放回去:

$data = file_get_contents('./users.txt'); 

$users = array_map(function($line) { 
    return explode(',', $line); 
}, explode("\n", $data)); 

foreach ($users as $i => $user) { 
    if ($user[0] == $username) { 
    $user[1] = $userpwd; 
    $users[$i] = $user; 
    } 
} 

file_put_contents('./users.txt', implode("\n", array_map(function($line) { 
    return implode(',', $line); 
}, $users))); 

當然有無數的方法可以做到這一點!