Reputation: 45
I m developing a CMS based website in which there is "Import Contact" functionality by clicking it user can select the .txt file which contains Contact Numbers. User can import unlimited .txt file but I want to add limit of importing contact numbers. for ex. 1.txt contains 100 contact numbers. & 2.txt contains 50 contact numbers.
& I had assigned 100 limit to the user, so whenever user imports the another .txt file it shoul throw error i.e "You have crossed your limit".
I had tried
<?php
$file = "somefile.txt";
$lines = count(file($file));
echo "There are $lines lines in $file";
?>
& this also;
$file="largefile.txt";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle, 4096);
$linecount = $linecount + substr_count($line, PHP_EOL);
}
fclose($handle);
echo $linecount;
this function i'm able to find out the contact numbers in .txt file.
But no able to compare two txt file & throw error..!!
Upvotes: 0
Views: 91
Reputation: 897
Firstly don't use text file, use a CSV file. If the conacts file contains multiple fields, which it will do, then you need to delimit them and CSV is the standard way to do that.
After that use:
$csv = array_map('str_getcsv', file('data.csv'));
Which will provide you with an array of array's - ie each record will be an array of fields which in turn will be in an array.
To get the numbe rof records in the file do a count of $csv
Upvotes: 2