PHP Change one line in a text file

I tried to find a solution, but I can not find the final one.

Basically, I have a txt file that lists usernames and passwords. I want to be able to change the password of a specific user.

The contents of the users.txt file:

user1,pass1 user2,pass2 user3,pass3 

I tried the following PHP code:

  // $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); 
+6
source share
4 answers

That should work! :)

 $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); 
+8
source

I think when you get this file, use file_get_contents after that, use preg_replace for a specific username

I have done this in the past, like here.

  $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); } 

Hope this helps ...

+2
source

The right way to do this is to use a database. Databases can do random access easily by doing this with text files.

If you cannot switch to the database for any reason, and you do not expect to have more than a thousand users for your system, then it would be much easier to just read the whole file, convert it to the PHP data structure, make the necessary changes, convert them back into text and overwrite the source file.

In this case, this means that the file () loads the text file into the array, each element of which is a username and password as a string, explodes all the elements of the array in comma to get the username and password separately, make the necessary changes, and then write Modified data to disk.

You can also find fgetcsv () useful for reading data. If you use SplFileObject and have the latest version of PHP, then fputcsv () may also be writable.

However, using a database is much better. The right tool for the job.

+1
source

If you are using * nix, you can use sed ; I find it tidier than playing with file descriptors, etc .:

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

If I did not agree with GordonM and parse the file in the PHP data structure, process it and then return it:

 $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))); 

Of course, there are many ways to do this!

0
source

Source: https://habr.com/ru/post/925741/


All Articles