Jump to content

checking for duplicates when writing to text file


cobusbo
Go to solution Solved by Barand,

Recommended Posts

Hi Im trying to write info to a text file and it works perfectly with the following script, but it writes duplicate info, how can I change it so that no duplicate records are being added to my file and just ignore inserting it without giving a message?

 

<?php
$myfile = fopen("users.csv", "a+") or die("Unable to open file!");
$mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"];
$txt = "$mxituid\n";
fwrite($myfile, $txt);
fclose($myfile);
?>
Edited by cobusbo
Link to comment
Share on other sites

Only possible if you first read the users.csv into memory and then scan every entry to see if there is a match. You should better store this into a database.

It doesn't have any private info in it the reason I want to store it as this file type is because I need to link the file to another website to publish something to all users saved in this file. So I don't want duplicate records

Link to comment
Share on other sites

 

Perhaps a combination of

  • file(), to read the csv file into an array and
  • array_unique(), to remove any duplicates

 

Thank you It solved my problem

 

 

<?php
$myfile = fopen("users.csv", "a+") or die("Unable to open file!");
$mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"];
$txt = "$mxituid\n";
fwrite($myfile, $txt);
fclose($myfile);




$list = file('users.csv');
$list = array_unique($list);
file_put_contents('unique.csv', implode('', $list));


?>
Link to comment
Share on other sites

There is no need to write an entry to users.csv and then read the contents of the file filtering out the duplicates and then writing the contents back to the file.

 

Instead. Read the contents of the file and and then only write the value in $_SERVER["HTTP_X_MXIT_USERID_R"] to the file if it does not exists

$file = 'users.csv';
$mxituid = $_SERVER["HTTP_X_MXIT_USERID_R"];

// read contents of file
$list = file($file, FILE_IGNORE_NEW_LINES);

// if the value is not logged
if(!in_array($mxituid, $list))
{
   // append the value to the file
   file_put_contents($file, $mxituid . PHP_EOL, FILE_APPEND);
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.