简体   繁体   中英

How to loop through each line in a file PHP

I am making a redeem key page for educational purposes with PHP just to see what it is capable of. I am storing the keys in a txt file as such:

key1
key2

And so on. I have tried the following to loop through the txt file, insert the values into and array and work off of there. Here is what I have:

$gkey = $_GET["key"];

$file = fopen("./generatedkeys.txt", "a");
$generatedk = array();

// generate table from data in txt file
while(! feof($file)) {
   $generatedk[] = fgets($file);
}
    

foreach ($generatedk as $key){
   if ($key == hash("sha256", $gkey)){

      // Removing of key from data in txt file
      $contents = file_get_contents($file);
      $contents = str_replace($key, '', $contents);
      file_put_contents($file, $contents);
      fclose($file);

      $accfile = fopen("./accs.txt", "a");
      fwrite($accfile, hash("sha256", $key).",".hash("sha256", $hwid)."\n");

      break;
   }
}

The code above however doesn't seem to be working. The key is simply not detected and not removed from generatedkeys.txt. (Not sure if there is any errors since I cannot see any). Is there any obvious mistakes? Any help is appreciated. Thank you.

I think this should make the process simpler, using file to read the whole file in one go into an array and then using array_search() to find if the key exists (it returns false if not found, so !== false ).

Then if found, it just appends the used key to the other file, unsets the array entry for the key and overwrites the original file...

$gkey = $_GET["key"];

$generatedk = file("./generatedkeys.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

if ( ($entry = array_search(hash("sha256", $gkey), $generatedk)) !== false )    {
    // Add key used, with whatever content you want
    file_put_contents("./accs.txt", $gkey.PHP_EOL, FILE_APPEND);
    // Remove found key from list in input file
    unset($generatedk[$entry]);
    // Overwrite input file with adjusted array
    file_put_contents("./generatedkeys.txt", implode(PHP_EOL, $generatedk));
}

Your code will make some empty lines when erases key from file. And you passed return value of fopen() as a parameter of file_get_contents(). the return value is resource on file, but file_get_contents() needs string(file path).

You can check following.

$gkey = $_GET["key"];

$file_path="./generatedkeys.txt";
$file = fopen($file_path, "a");
$generatedk = array();

// generate table from data in txt file
while(! feof($file)) {
   $generatedk[] = fgets($file);
}
fclose($file);

for($i=0; $i<count($generatedk); $i++){
   $key=$generatedk[$i];
   if ($key == hash("sha256", $gkey)){

      // Removing of key from data in txt file
      array_splice($generatedk, $i, 1);
      file_put_contents($file_path, implode("\n", $generatedk));

      $accfile = fopen("./accs.txt", "a");
      fwrite($accfile, hash("sha256", $key).",".hash("sha256", $hwid)."\n");
      fclose($accfile);

      break;
   }
}

I hope it will be working well. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM